-2

In a folder, I have two files— a Python Script and an Executable application. What I want is to run the Executable app using the Python script. I used below pieces of code:

os.chdir("D:\\ToneFinder GIthub\\ToneFinder\\Record.exe") # NotADirectoryError: [WinError 267] The directory name is invalid: 'D:\\ToneFinder GIthub\\ToneFinder\\Record.exe'
os.system("D:\\ToneFinder GIthub\\ToneFinder\\Record.exe") # 'D:\ToneFinder' is not recognized as an #internal or external command,
operable program or batch file.

os.startfile("D:\\ToneFinder GIthub\\ToneFinder\\Record.exe") # Nothing happens

subprocess.call("D:\\ToneFinder GIthub\\ToneFinder\\Record.exe") #Error loading Python DLL 'D:\ToneFinder GIthub\ToneFinder\python310.dll'

Note: Always, the path is 100% correct

Any ideas on that? Thank you.

JKR
  • 69
  • 1
  • 1
  • 10
  • The chdir wouldn't work... you change to the directory `"D:\\MyTestApp\\MyTestApp"` , not the executable file in that directory. The path is 100% correct, but the error message has a different path name in it... that is puzzling. Is D:\\MyTestApp a symlink? – tdelaney Apr 07 '22 at 17:24
  • I'm really sorry for the mistake, the path in the error is correct. Just edited it! – JKR Apr 07 '22 at 17:33
  • As mentioned below, you need to escape the spaces in the path name. Usually that can be done by adding quotes for the windows shell. Note the single quotes for python, then the doubles for windows. `os.startfile('"D:\\ToneFinder GIthub\\ToneFinder\\Record.exe"')` – tdelaney Apr 07 '22 at 21:33

1 Answers1

1

chdir

os.chdir is for changing the working directory, not running a program. ...\Record.exe is not a directory.

system, startfile

The path contains a space, so Windows tries to run D:\ToneFinder (which implicitly becomes D:\ToneFinder.exe) with the argument GIthub\ToneFinder\Record.exe. Since there is no such file, an error occurs. Surrounding it with double quotes (os.system(r'"D:\ToneFinder GIthub\ToneFinder\Record.exe"')) will cause it to be interpreted as one long path.

call

subprocess.call takes a list of space-separated arguments, so it should be called like so: subprocess.call(r'"D:\ToneFinder GIthub\ToneFinder\Record.exe"'.split())

Which one should I use?

See Difference between subprocess.Popen and os.system.

Makonede
  • 442
  • 1
  • 6
  • 13
  • Hey there, @Makonede. Thanks for the explantation but still, I can't figure out what to use. As in the answer you mentioned, I tired subprocess.Popen, but it returned: `Error loading Python DLL 'D:\ToneFinder GIthub\ToneFinder\python310.dll'. LoadLibrary: The specified module could not be found. – JKR Apr 07 '22 at 17:35
  • @JKR Try `os.system` then. – Makonede Apr 07 '22 at 17:37
  • It returns another Error: `'D:\ToneFinder' is not recognized as an internal or external command, operable program or batch file. – JKR Apr 07 '22 at 17:38
  • @JKR Did you even read my answer? – Makonede Apr 08 '22 at 00:17
  • I'm sorry, I tried it right now. But, then another error: `Error loading Python DLL 'D:\ToneFinderGIthub\ToneFinder\python310.dll'. LoadLibrary: The specified module could not be found.` – JKR Apr 08 '22 at 03:00
  • @JKR I told you, use `system` if `Popen` doesn't work. – Makonede Apr 08 '22 at 15:31