user2334659
user2334659

Reputation: 144

Issue with using os.system to call another script

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?

Upvotes: 0

Views: 398

Answers (1)

user2334659
user2334659

Reputation: 144

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.

Upvotes: 1

Related Questions