Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让我的脚本在windows上使用python每30分钟重复一次_Python_Python 3.x_Time_Automation - Fatal编程技术网

如何让我的脚本在windows上使用python每30分钟重复一次

如何让我的脚本在windows上使用python每30分钟重复一次,python,python-3.x,time,automation,Python,Python 3.x,Time,Automation,我有下面的纸条,我希望它每30分钟运行一次。有人能告诉我正确的方向吗 我已经搜索过像这样的现有问题,但似乎没有找到任何与我的脚本相关的想法,但不知道这是否是我的愚蠢 我的脚本会在屏幕上的不同位置单击,然后进行屏幕截图,然后将图像发送到我的gmail帐户 import pyautogui import time import smtplib from email.mime.multipart import MIMEMultipart from email.mime.base import MIME

我有下面的纸条,我希望它每30分钟运行一次。有人能告诉我正确的方向吗

我已经搜索过像这样的现有问题,但似乎没有找到任何与我的脚本相关的想法,但不知道这是否是我的愚蠢

我的脚本会在屏幕上的不同位置单击,然后进行屏幕截图,然后将图像发送到我的gmail帐户

import pyautogui
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
import os


time.sleep(5)
pyautogui.PAUSE = 1
pyautogui.moveTo(922,134)
pyautogui.click()
pyautogui.PAUSE = 1
pyautogui.moveTo(178,277)
pyautogui.click()
pyautogui.PAUSE = 1
pyautogui.moveTo(178,297)
pyautogui.click()
pyautogui.PAUSE = 1
pyautogui.moveTo(178,315)
pyautogui.click()
pyautogui.PAUSE = 1
pyautogui.screenshot('web.png')

pyautogui.PAUSE = 5

gmail_user = "user@gmail.com"
gmail_pwd = "password"

to = "user@gmail.com"
subject = "Report"
text = "Picture report"
attach = 'web.png'

msg = MIMEMultipart()

msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject

msg.attach(MIMEText(text))

part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',
   'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)

mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()

您的循环应该如下所示:

while '1' == '1':
    '''
    Your script that should loop here
    '''
    time.sleep(1800)
使用windows任务:

schtasks /create /sc minute /mo 30 /tn "PyAutoGUI Task" /tr "python <path to script>"
schtasks/create/sc minute/mo 30/tn“PyAutoGUI任务”/tr“python”

假设原始脚本的文件名为testfile.py

使用以下代码在与第一个文件相同的目录中创建另一个文件:

import time,os
while 1:
    time.sleep(1800)
    os.system("start python testfile.py")
运行此脚本将每隔30分钟运行另一个文件,而不管脚本运行多长时间

  • 包装程序(如果需要,请确保通过设置环境变量提供凭据)
  • 它命令处理器运行python程序
  • 触发批处理文件
  • 我相信time.sleep(),如其余答案中所述,即使不使用,也会占用处理器

    我有一个类似的要求,我必须运行一段代码,但也要每30分钟连接一次服务器,否则会超时

    这就是我所做的

    import time
    start = time.time()
    process_time = 0
    max_processing_time = 20000
    sleep_time = 1800
    
    while process_time < max_processing_time:
        if (time.time() - start) > sleep_time:
            start = time.time()
            service = service_connect() ## Connect to server every 30 minutes
        
        ## Otherwise keep on running this code
        function_which_I_want_to_run_continously()
    
    导入时间
    开始=时间。时间()
    处理时间=0
    最大处理时间=20000
    睡眠时间=1800
    当处理时间<最大处理时间时:
    如果(time.time()-start)>睡眠时间:
    开始=时间。时间()
    service=service_connect()##每30分钟连接一次服务器
    ##否则,请继续运行此代码
    我希望继续运行的函数()
    

    在Scenario中,可以在“if”块中插入代码段。您可以根据需要设置最大处理时间和睡眠时间。

    为什么不创建一个每30分钟执行一次脚本的任务?为什么不使用
    时间将代码包装在无限循环中。睡眠(30*60)
    ?Linux有crontab。我从未在Windows上使用过类似的东西,但该链接似乎有几种可能的解决方案。嗨,我是python新手,所以我仍在学习,所以不太确定如何将其作为任务编写或将其放入无限循环中。你能给我一个例子吗请复制
    ,而True:…
    是它的更好版本。这只是我的一个习惯,我真的需要打破它当我运行上面的脚本时,它就会打开close@scott.turner你需要导入时间才能让它工作,请看我的我只是在编写你的代码@scott.turner,我只是在循环中给你剩余代码(不包括导入)的位置。将导入内容从这段代码中排除,您不需要让它继续导入。这是更好的解决方案。您好,我会这样做吗?挑剔:如果任务本身持续20分钟,这不会每30分钟运行一次脚本,而是每50分钟运行一次。@Jean Françoisfare我已将其更改为每20分钟运行一次,而不管运行所需的时间如何。这可能不是一个可行的解决方案。即使python程序在指定的时间内休眠,也只是要求python不要执行任何操作,这意味着python编译器仍在使用处理器。如果程序执行仅持续10秒怎么办?你牺牲了29分钟50秒的处理器。嗨,如果我创建了一个exe文件,我可以这样做吗