In a 64-bit system with 32 bit python 2.7 installed I am trying to do the following:
import subprocess
p = subprocess.call('dir', shell=True)
print p
But this gives me:
Traceback (most recent call last):
File "test.py", line 2, in <module>
p = subprocess.call('dir', shell=True)
File "C:\Python27\lib\subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 709, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 957, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
If I in the terminal do...
dir
...it of course prints the present folder content.
I have tried to change the shell parameter to shell=False.
Edit: Actually I cannot call any executable on the path with subprocess.call()
. The statement p = subprocess.call('dir', shell=True)
works fine on another machine and I think that it is related.
If I do
subprocess.call('PATH', shell=True)
then I get
Traceback (most recent call last):
File "test.py", line 4, in <module>
subprocess.call('PATH', shell=True)
File "C:\Python27\lib\subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 709, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 957, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
If I do:
import os
print os.curdir
then I get
.
All of the above is executed in the terminal started in Administrator mode.
After reading the docs, I came to know that shell=True means executing the code through the shell. So that means in absence, the process is directly started.
chdir() or with the subprocess named parameter cwd which changes the working directory immediately before executing a subprocess.
By default, running subprocess. Popen with shell=True uses /bin/sh as the shell. If you want to change the shell to /bin/bash , set the executable keyword argument to /bin/bash .
Subprocess in Python is a module used to run new codes and applications by creating new processes. It lets you start new applications right from the Python program you are currently writing. So, if you want to run external programs from a git repository or codes from C or C++ programs, you can use subprocess in Python.
I think you may have a problem with your COMSPEC
environment variable:
>>> import os
>>> os.environ['COMSPEC']
'C:\\Windows\\system32\\cmd.exe'
>>> import subprocess
>>> subprocess.call('dir', shell=True)
(normal output here)
>>> os.environ['COMSPEC'] = 'C:\\nonexistent.exe'
>>> subprocess.call('dir', shell=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\Python27\lib\subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "c:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "c:\Python27\lib\subprocess.py", line 896, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
I discovered this potential issue by digging into subprocess.py
and looking in the _execute_child
function, as pointed-to by the traceback. There, you'll find a block starting with if shell:
that will search the environment for said variable and use it to create the arguments used to launch the process.
Before downvote, note that the question was edited after i posted this answer.
I think os.listdir
is more suitable for your case:
>>> import os
>>> os.listdir()
['1.txt', '2.txt', '3.txt', 'DLLs', 'Doc', 'e.txt', 'include', 'Lib', 'libs', 'LICENSE.txt', 'm.txt', 'msvcr100.dll', 'NEWS.txt', 'py.exe', 'python.exe', 'python33.dll', 'pythonw.exe', 'pyw.exe', 'README.txt', 'Scripts', 't.txt', 'tcl', 'Tools']
If you want to run it in the command line itself, and just feeling like to call it, you can use os.sytem
:
os.system('dir')
This will run the commmand, but it returns 0
and you can't store it.
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