Python3x - Write a python script to run other python scripts? -


i have number of python scripts automate using python's datetime , schedule module.

they numerous consider breaking apart , adding 1 large file.

what easiest way write python script open , run these other python scripts?

i have browsed similar questions none offered concrete answer find. help.

a minimally demonstrative example

in file called "child.py", write file current directory:

with open('test', 'w') f:     f.write('hello world') 

then, in file called "parent.py", execute "child.py" script:

import subprocess  subprocess.call(['python', 'child.py']) 

now, command line, can type (assuming both "parent.py" , "child.py" in current directory):

python parent.py 

in next instant, should see file called "test" in current directory. open up. see?

well, hello world of course!

the above example makes child of current process (meaning inherits environment variables in parent), , waits until child process completes before returning control parent. if want child script run in background, need use popen:

subprocess.popen(['python', 'child.py']) 

Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -