I write a simple script to check the subprocess module and I tested it on both Windows and Linux. The script works fine on Windows but not on Linux.
To capture the output of the subprocess. run method, use an additional argument named “capture_output=True”. You can individually access stdout and stderr values by using “output. stdout” and “output.
Now, use a simple example to call a subprocess for the built-in Unix command “ls -l.” The ls command lists all the files in a directory, and the -l command lists those directories in an extended format. As simple as that, the output displays the total number of files along with the current date and time.
When the command is a shell built-in, add a shell=True
to the call.
E.g. for dir
you would type:
import subprocess
subprocess.call('dir', shell=True)
To quote from the documentation:
The only time you need to specify
shell=True
on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not needshell=True
to run a batch file or console-based executable.
On Windows, I believe the subprocess
module doesn't look in the PATH
unless you pass shell=True
because it use CreateProcess()
behind the scenes. However, shell=True
can be a security risk if you're passing arguments that may come from outside your program. To make subprocess
nonetheless able to find the correct executable, you can use shutil.which
. Suppose the executable in your PATH
is named frob
:
subprocess.call([shutil.which('frob'), arg1, arg2])
(This works on Python 3.3 and above.)
On Windows you have to call through cmd.exe. As Apalala mentioned, Windows commands are implemented in cmd.exe not as separate executables.
e.g.
subprocess.call(['cmd', '/c', 'dir'])
/c tells cmd to run the follow command
This is safer than using shell=True, which allows shell injections.
If you are using powershell, then in it will be subprocess.call(['powershell','-command','dir'])
. Powershell supports a large portion of POSIX commands
After much head scratching, I discovered that running a file that is located in C:\Windows\System32\ while running a 32bit version of python on a 64bit machine is a potential issue, due to Windows trying to out-smart the process, and redirect calls to C:\Windows\System32 to C:\Windows\SysWOW64.
I found an example of how to fix this here: http://code.activestate.com/recipes/578035-disable-file-system-redirector/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With