Вопрос

I have a Python Script that I am trying to write to run other Python Scripts. 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.

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. 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!

Это было полезно?

Решение

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.

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 supports this via the

if __name__ == "__main__":

idiom.

ReportN.py would be written as:

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.

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.

Другие советы

Your subprocess.call() is incorrect, see the proper syntax here: 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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top