【发布时间】:2014-05-14 22:31:17
【问题描述】:
我有一个 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
我能得到的任何帮助都会很棒!
【问题讨论】:
-
对于初学者,将行
t = Timer(60.0, checkTime(fileRan))更改为t = Timer(60.0, checkTime, fileRan)(因为计时器必须调用函数本身,而您调用函数然后传递结果) -
按照
t = Timer(60.0, checkTime,fileRan)的建议进行更改现在收到此错误。TypeError: checkTime() argument after * must be a sequence, not bool -
试试
t = Timer(60.0, checkTime, [fileRan]),并在我的回答中查看编辑。 -
简单解决方法请参考以下链接simple solution using os.system in python