简体   繁体   English

Python脚本以运行其他Python脚本

[英]Python Script to run other Python Scripts

I have a Python Script that I am trying to write to run other Python Scripts. 我有一个要尝试运行其他Python脚本的Python脚本。 The goal is to be able to have a script I run all the time to execute my other scripts overnight. 目的是使我可以一直运行一个脚本,以在一夜之间执行其他脚本。 (I tried this using a batch file, and it would execute them, but they would not create the .csv files for some reason) Here is the code I have now. (我使用批处理文件尝试了此操作,它会执行它们,但是由于某种原因它们不会创建.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()

It will make the first pass and print the current time, and the state of fileRan just fine. 它将进行第一次通过并打印当前时间,并且fileRan的状态就可以了。 It appears to break either when I make the second check, or when it tried to execute the files. 当我进行第二次检查或尝试执行文件时,它似乎断开。 Here is the error I am getting: 这是我得到的错误:

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

Any help I could get would be great! 我能得到的任何帮助将是巨大的!

Your subprocess.call() is incorrect, see the proper syntax here: https://docs.python.org/2/library/subprocess.html 您的subprocess.call()错误,请在此处查看正确的语法: https : //docs.python.org/2/library/subprocess.html

subprocess.call(["python", "report1.py"], shell=True)

There are also other tools to do this for you, such as cron . 还有其他工具可以帮助您完成此任务,例如cron

This isn't an answer to your problem, but a coding suggestion. 这不是您问题的答案,而是编码建议。 Calling Python scripts from other Python scripts should be considered a last-ditch approach. 从其他Python脚本调用Python脚本应被视为最后的方法。

Answer to similar question 回答类似问题

The preferred methodology for calling Python files from Python files is to design the 'reportsN.py' files to be usable as both a library and a command-line callable. 从Python文件中调用Python文件的首选方法是将“ reportsN.py”文件设计为既可以用作库又可以用作命令行调用。 Python supports this via the Python通过

if __name__ == "__main__":

idiom. 成语。

ReportN.py would be written as: ReportN.py将写为:

def stuff_to_do():
    pass

if __name__ == "__main__":
    stuff_to_do()

Your top script, 'run_files.py' would handle importing each 'reportN.py' file as a library and allocating their stuff_to_do() functions/methods to threads as needed. 您的顶级脚本“ run_files.py”将处理将每个“ reportN.py”文件作为库导入,并根据需要将其stuff_to_do()函数/方法分配给线程。

This approach is not always possible (if 'reportN.py' is not under your control, for example), but this approach will simplify your problems by taking 'subprocess' out of the list of things you have to contend with. 这种方法并非总是可行的(例如,如果'reportN.py'不在您的控制之下),但是该方法通过将'subprocess'从您必须解决的事情列表中删除,从而简化了您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM