Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Errno 2: No such file or directory

Tags:

python

cmd

errno

So, im trying to use subprocess.Popen() to open a python script in CMD the "complicated" way. Although I cant get it to open due to a space in my PC name. Ive tried using Double Quotes and Single Quotes vut it still doesn't work.

Heres the line of code im trying to execute.

subprocess.Popen("cmd.exe /C python '\Users\Terra Byte\Desktop\jdos3\JDOS3\SYS64\bootthingy.py'")

As you can see, I'm using single quotes to wrap the directory path, yet this is the error I get when executing.

C:\Users\Terra Byte\Desktop\jdos3\JDOS3>python: can't open file ''\Users\Terra': [Errno 2] No such file or directory

It seems to be completely ignoring my single quotes.

like image 399
itz_lexiii_ Avatar asked Jul 07 '26 06:07

itz_lexiii_


1 Answers

Never try to build your command name as a string.

In this particular case, be aware that single quotes have no protection effect on windows (unlike on Linux/Unix) which explains the quoting you've used is inefficient. Using double quotes would have worked, but it's not the best way.

Never use a string when you can pass the list of arguments. This will work:

subprocess.Popen(["python",r'\Users\Terra Byte\Desktop\jdos3\JDOS3\SYS64\bootthingy.py'])
  • use list of strings, unquoted, and let subprocess do the work
  • remove cmd /c prefix, as python prefix is enough (alternately, remove python to leave ["cmd","/c" and let file associations do the work)
  • use raw string prefix to avoid that backslashes are interpreted
like image 184
Jean-François Fabre Avatar answered Jul 08 '26 20:07

Jean-François Fabre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!