Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 420
alphanumeric Avatar asked Dec 08 '13 17:12

alphanumeric


2 Answers

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. ;)

like image 98
Max Noel Avatar answered Sep 26 '22 03:09

Max Noel


Either use:

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

or

cmd = r'"C:\Program Files (x86)\iTunesr\iTunes.exe"'
like image 29
Steve Barnes Avatar answered Sep 24 '22 03:09

Steve Barnes