[英]Python and windows scheduler - run py script that invokes other scripts
[英]Python Script to run other Python Scripts
我有一个要尝试运行其他Python脚本的Python脚本。 目的是使我可以一直运行一个脚本,以在一夜之间执行其他脚本。 (我使用批处理文件尝试了此操作,它会执行它们,但是由于某种原因它们不会创建.csv文件)这是我现在的代码。
import time
import subprocess
from threading import Timer
fileRan = False
#Function to launch other python files
def runFiles():
print('Running Scripts Now')
subprocess.call("cmd","report1.py",shell=True)
subprocess.call("cmd","report2.py",shell=True)
subprocess.call("cmd","report3.py",shell=True)
subprocess.call("cmd","report4.py",shell=True)
subprocess.call("cmd","report5.py",shell=True)
subprocess.call("cmd","report6.py",shell=True)
#Function to check current system time against time reports should run
def checkTime(fileRan):
startTime = '15:20'
endTime = '15:25'
print('Current Time Is: ', time.strftime('%H:%M', time.localtime()))
print(fileRan)
if startTime < time.strftime('%H:%M', time.localtime()) < endTime and fileRan is False:
runFiles()
fileRan = True
return fileRan
#Timer itself
t = Timer(60.0, checkTime(fileRan))
t.start()
它将进行第一次通过并打印当前时间,并且fileRan的状态就可以了。 当我进行第二次检查或尝试执行文件时,它似乎断开。 这是我得到的错误:
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Python34\lib\threading.py", line 921, in _bootstrap_inner
self.run()
File "C:\Python34\lib\threading.py", line 1187, in run
self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable
我能得到的任何帮助将是巨大的!
您的subprocess.call()错误,请在此处查看正确的语法: https : //docs.python.org/2/library/subprocess.html
subprocess.call(["python", "report1.py"], shell=True)
还有其他工具可以帮助您完成此任务,例如cron 。
这不是您问题的答案,而是编码建议。 从其他Python脚本调用Python脚本应被视为最后的方法。
从Python文件中调用Python文件的首选方法是将“ reportsN.py”文件设计为既可以用作库又可以用作命令行调用。 Python通过
if __name__ == "__main__":
成语。
ReportN.py将写为:
def stuff_to_do():
pass
if __name__ == "__main__":
stuff_to_do()
您的顶级脚本“ run_files.py”将处理将每个“ reportN.py”文件作为库导入,并根据需要将其stuff_to_do()函数/方法分配给线程。
这种方法并非总是可行的(例如,如果'reportN.py'不在您的控制之下),但是该方法通过将'subprocess'从您必须解决的事情列表中删除,从而简化了您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.