I am working on another issue which requires me to use os.system() to call another python script. I know that subprocess is the better solution but for what I'm trying to do but I can't use that and am stuck with os.system(). I set up a small test program as follows to try and figure out os.system():
sript1.py
import os
import sys
os.system("C:/Users/user/Documents/code/test/called_script.py")
called_script.py
import os
import sys
sourceFile = open('C:/Users/user/Documents/code/test/test.txt', 'w')
print("hi", file = sourceFile)
sourceFile.close()
If I run script1.py it will not create (or if it already exists write to) test.txt
But if I run called_script.py directly it will create/write to the file test.txt
I'm sure I'm overlooking something simple, but could someone help me out with why running script1.py is not getting me the desired outcome?
Thanks to the above comments, the solution is that script1.py should read
os.system("py C:/Users/user/Documents/code/test/called_script.py")
Because Windows does not know what to do with a .py file otherwise.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.