2

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.

Kyle Clark
  • 43
  • 5
  • 2
    You should probably replace `os.system` with `subprocess.run` in any case, but I don't see how this could have stopped working if it was working previously. The error message suggests that whatever shell `os.system` is using behind the scenes has stopped treating this as a single path and started treating it as a path plus an argument, split on the space between `Test` and `UI`. – chepner Jan 06 '21 at 16:42
  • That is strange.It seems confused with the space in the name. You could try `os.system(r'"C:\Users\red\Desktop\Test UI"')` - I just encased the string in single quotes so that the double quotes actually go to the windows process execute. – tdelaney Jan 06 '21 at 16:46
  • ...um I'm assuming the file to run is "Test UI.exe" – tdelaney Jan 06 '21 at 16:47
  • 2
    Note that in the answer on the linked question, `subprocess.call` is being used, not `os.system`. When you aren't using `shell=True` on the former, these are very different things. `subprocess.call` doesn't try to do any kind of string-splitting with the default `shell=False`, whereas `os.system` expects everything to be passed in the local shell's syntax (which requires either quoting or escaping whitespace, though the details of how are platform-specific). – Charles Duffy Jan 06 '21 at 16:57
  • @tdelaney It is actually just Test UI, it's a shortcut that opens a google chrome tab and brings me to a certain page. I tried using your single quotes and then double quotes method like you recommended, but I still get the same error as mentioned in the question. – Kyle Clark Jan 06 '21 at 17:08
  • So its a program called "Test" with a parameter "UI"? – tdelaney Jan 06 '21 at 17:12
  • You could `from glob import glob;print(glob(r"C:\Users\red\Desktop\Test*"))` so that we know what the acutal file looks like. – tdelaney Jan 06 '21 at 17:14
  • @tdelaney It is a shortcut, not a program, on my desktop called Test UI, when it worked a few days ago it opened up a google chrome tab and brought me to the page I wanted. – Kyle Clark Jan 06 '21 at 17:16

3 Answers3

4

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)
RiskyMick
  • 151
  • 6
  • I tried this suggestion and got the error "FileNotFoundError: [WinError 2] The system cannot find the file specified." – Kyle Clark Jan 06 '21 at 17:22
  • Code above is tested on python 3.7.2. My code that works is below `subprocess.run(r'"c:\users\myname\Desktop\AD Explorer.exe"')` also works without the .exe on the end and with , shell=True – RiskyMick Jan 06 '21 at 18:34
  • To get this working with Windows shortcuts I had to actually add the .lnk on the end `subprocess.run(r'"c:\users\myname\Desktop\Postman.lnk"',shell=True)` – RiskyMick Jan 06 '21 at 18:40
2

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")
Kyle Clark
  • 43
  • 5
0

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.

yourson
  • 88
  • 1
  • 4
  • 18
  • The file I want to execute is indeed in that directory. The file name is correct, it is supposed to execute a shortcut that opens a google chrome tab that brings me to a specific page. – Kyle Clark Jan 06 '21 at 17:14
  • Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under a [CC BY-SA license (2.5/3.0/4.0)](//stackoverflow.com/help/licensing), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](//meta.stackexchange.com/q/5221) – Sabito stands with Ukraine Jan 31 '21 at 04:04