Reputation: 43
A few days ago I had the line os.system(r"C:\Users\red\Desktop\Test UI")
in my program. I tested it and it worked fine, it opened the application just like I wanted it to.
Now, I'm coming back to it about five days later and all of a sudden it's not working properly. I checked the processes and it says 'C:\Users\red\Desktop\Test' is not recognized as an internal or external command, operable program, or batch file.
I have already looked at the other questions about os.system like How do I execute a program from Python? os.system fails due to spaces in path, but I am already using a raw string like one of the answers suggested. I don't understand how it can work one day, and the next it fails to work with no change to it.
Upvotes: 2
Views: 3630
Reputation: 86
Actually, you are using right command to execute a file, but the file you want to execute is not present in that directory. The error 'C:\users...' is not not recognized as an internal or external command, operable program, or batch file
occurs when the file is not in the directory path.
There is no need to write .exe
after file name, but you must write the correct file name which is present in that directory.
Upvotes: 0
Reputation: 43
I figured it out. For some reason, os.system stopped working consistently and subprocess.run or subprocess.call didn't work. I switched my command to use os.startfile instead and it started to work properly.
Here's the end result:
os.startfile(r"C:\Users\red\Desktop\Test UI")
Upvotes: 2
Reputation: 151
We've recently replaced os.system with subprocess.run after a number of problems with paths on Windows.
For this example, you could replace
os.system(r"C:\Users\red\Desktop\Test UI")
with
subprocess.run(r'"C:\Users\red\Desktop\Test UI"',shell=True)
For Windows shortcuts I had to add the .lnk extension in the call:
subprocess.run(r'"C:\Users\red\Desktop\Test UI.lnk"',shell=True)
Upvotes: 4