Search code examples
pythonsubprocesspopen

Python, subprocess, filepath white spaces and famous 'C:/Program' is not recognized as an internal or external command


Enclosing a full file path inside the "" quotes does not make it work.

cmd = "C:\Program Files (x86)\iTunes\iTunes.exe"

subprocess.popen throws an error of not being able to find an executable if there is a white space in a file-path to be executed.

A while ago I was able to find a solution which involved a use of some weird symbols or their combination... Unfortunately I can't locate a code with that example. I would appreciate if someone would point me in a right direction. Thanks in advance.


Solution

  • Backslashes in strings trigger escape characters. Since Windows fully supports the use of the forward slash as a path separator, just do that:

    cmd = "C:/Program Files (x86)/iTunes/iTunes.exe"
    

    No need to fiddle around with \\ or raw strings. ;)