Spaces in path name

joep josef.pktd at gmail.com
Sun Mar 16 13:00:12 EDT 2008


One more try (using popen instead of call is not necessary for these
cases, but I want to see the behavior of popen):

shell=True and executable and at least one argument with spaces does
not work:
---------------------------------------------------------------------------------------------------------------------

p = subprocess.Popen([r"C:\Programs\GnuWin32\GetGnuWin32\gnuwin32\bin
\Copy of cp.exe",
                      r"C:\Temp\temp with spaces\test out.txt",
                      r"C:\Temp\temp with spaces\test out3.txt"],
                     shell=True, stdout=subprocess.PIPE)

causes:
'C:\Programs\GnuWin32\GetGnuWin32\gnuwin32\bin\Copy' is not recognized
as an int
ernal or external command, operable program or batch file.


the following all work:
================

without shell=True, and executable and at least one argument with
spaces
-----------------------------------------------------------------------------------------------------------

p = subprocess.Popen([r"C:\Programs\GnuWin32\GetGnuWin32\gnuwin32\bin
\Copy of cp.exe",
                      r"C:\Temp\temp with spaces\test out.txt",
                      r"C:\Temp\temp with spaces\test out4.txt"],
                     stdout=subprocess.PIPE)

with shell=True, and executable without spaces, even if two arguments
with spaces
------------------------------------------------------------------------------------------------------------------------

p = subprocess.Popen(["copy",
                      r"C:\Temp\temp with spaces\test out.txt",
                      r"C:\Temp\temp with spaces\test out2.txt"],
                     shell=True, stdout=subprocess.PIPE)

with shell=True, and executable without spaces, even if two arguments
with spaces
------------------------------------------------------------------------------------------------------------------------
but here shell=True is not necessary

p = subprocess.Popen([r"C:\Programs\GnuWin32\GetGnuWin32\gnuwin32\bin
\cp.exe",
                      r"C:\Temp\temp with spaces\test out.txt",
                      r"C:\Temp\temp with spaces\test out5.txt"],
                     shell=True, stdout=subprocess.PIPE)

without shell=True, and executable and at least one arguments with
spaces
-------------------------------------------------------------------------------------------------------------

p = subprocess.Popen([r"C:\Programs\GnuWin32\GetGnuWin32\gnuwin32\bin
\Copy of cp.exe",
                      r"C:\Temp\temp with spaces\test out.txt",
                      r"C:\Temp\temp with spaces\test out4.txt"],
                     stdout=subprocess.PIPE)

My conclusions:
============

Assuming shell=True is only required for build-in shell commands, non
of which has spaces.

There is no problem, if you know when *not* to use shell=True:

 * More than two arguments with spaces are never problem, as long as
the executable does not have spaces
* If shell=True is required, then the executable is a build-in shell
command, which does not contain spaces, and, therefore, has no
problems
* If you use a non-build in executable, then don't use shell=True.
This works correctly even if the executable and at least one
additional argument have spaces.

It took a lot of time to figure this out, but now at least I know, how
to construct the call to subprocess.Popen, so that it works in the
cases I used it so far.

Josef



More information about the Python-list mailing list