Section 17.1.1.1. of the documentation states:
If shell is True, the specified command will be executed through the shell. This can be useful if you are using Python primarily for the enhanced control flow it offers over most system shells and still want convenient access to other shell features such as shell pipes, filename wildcards, environment variable expansion, and expansion of ~ to a user’s home directory.
However, on cygwin, output from bash isn't the same as from Python's subprocess, viz:
Bash:
$ ls -ls ~rbarakx
total 0
0 drwxr-xr-x 1 Administrator None 0 Aug 21 17:54 bash
0 drwxr-xr-x 1 Administrator None 0 Jul 11 09:11 python
Python:
>>> subprocess.call(["ls","-ls","~rbarakx"],shell=True)
RCS mecha.py print_unicode.py req.py requests_results.html selen.py
0
It looks as if subprocess.call is executing just ls.
Can you suggest why?
My Environment:
Python: Python 2.7.3 (default, Dec 18 2012, 13:50:09) [GCC 4.5.3] on
cygwin
cygwin: CYGWIN_NT-6.1-WOW64 ... 1.7.22(0.268/5/3) ... i686
Cygwin
windows: Windows 7 Ultimate
On Windows, the shell (
cmd.exe
) doesn't support the expansion of~
to a user's home directory. Nor wildcard expansion, for that matter. The Python doc is very UNIX/Linux-oriented on this point."But wait!" I hear you cry. "I've installed
cygwin
, I havebash
!" Sure you have, but you can hardly expect Python to know that. You're on Windows, so it's going to use the Windows shell. If it didn't, Python scripts that expected the shell to becmd.exe
would be mighty confused.Interestingly when passing the command as a string instead of a tuple, it seems to work as if you were executing it in bash.
If you insist on using tuple, consider the following workaround:
or this:
Because on Unix (cygwin) your call is equivalent to:
In other words the argument list is passed to the shell itself i.e.,
ls
doesn't see-ls
or~rbarakx
they are considerred/bin/sh
arguments.What you want is:
Or using
shell=True
:Here's the quote from
subprocess
docs for reference:emphasis is mine
You could also execute the command without any shell: