1

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!

JasonWH
  • 179
  • 2
  • 17
  • For starters, change the line `t = Timer(60.0, checkTime(fileRan))` to `t = Timer(60.0, checkTime, fileRan)` (as the timer has to call the function itself, and you're calling the function then passing the result) – David Robinson May 14 '14 at 22:34
  • Made the change as suggested this `t = Timer(60.0, checkTime,fileRan)` Now getting this error. `TypeError: checkTime() argument after * must be a sequence, not bool` – JasonWH May 14 '14 at 22:41
  • Try `t = Timer(60.0, checkTime, [fileRan])`, and see the edits in my answer as well. – Nick Sweeting May 14 '14 at 22:46
  • Simple solution please refer to the following link [simple solution using os.system in python](http://stackoverflow.com/a/35726321/1965509) – Snehal Parmar Mar 01 '16 at 15:05

2 Answers2

2

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.

Nick Sweeting
  • 5,364
  • 5
  • 27
  • 37
  • There should be a space between "python" and "report1.py". – Zeeshan Jan 13 '16 at 06:14
  • @ZeeshanMahmood, no there should not be a space. subprocess.call takes a list of the command and its arguments or a single string with them all together. If you pass a list, it does the job of adding in spaces automatically. Read the docs I linked to in my answer. – Nick Sweeting Jan 13 '16 at 18:30
  • 1
    you are right. I missed noticing that it is a list. Thanks. – Zeeshan Jan 14 '16 at 06:09
2

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.

Community
  • 1
  • 1
JS.
  • 14,781
  • 13
  • 63
  • 75