How can I make one python file to run another?
For example I have two .py files. I want one file to be run, and then have it run the other .py file.
This question is related to
python
import os
os.system('python filename.py')
note: put the file in the same directory of your main python file.
It may be called abc.py
from the main script as below:
#!/usr/bin/python
import abc
abc.py
may be something like this:
print'abc'
I used subprocess.call it's almost same like subprocess.Popen
from subprocess import call
call(["python", "your_file.py"])
You'd treat one of the files as a python module and make the other one import it (just as you import standard python modules). The latter can then refer to objects (including classes and functions) defined in the imported module. The module can also run whatever initialization code it needs. See http://docs.python.org/tutorial/modules.html
Put this in main.py:
#!/usr/bin/python
import yoursubfile
Put this in yoursubfile.py
#!/usr/bin/python
print("hello")
Run it:
python main.py
It prints:
hello
Thus main.py
runs yoursubfile.py
There are 8 ways to answer this question, A more canonical answer is here: How to import other Python files?
from subprocess import Popen
Popen('python filename.py')
You could use this script:
def run(runfile):
with open(runfile,"r") as rnf:
exec(rnf.read())
Syntax:
run("file.py")
Source: Stackoverflow.com