Cannot find the file specified when using subprocess.call('dir', shell=True) in Python

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.

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.

Subprocess.call does not work! - Support, Hi all, I try to use subprocess.call to run an external application in the python WindowsError: [Error 2] The system cannot find the file specified. It happens that the CMD shell strips quotes out, but that doesn't make it valid. PATH in Windows is delimited by semicolons, not spaces, so paths with spaces should never be quoted. In particular, WINAPI SearchPathW, which CreateProcessW calls, leaves quotes in the directory name when testing whether a file is in the directory.

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.

The system cannot find the file specified � Issue #337 � sublimelsp , However, the Python function subprocess.Popen failes to start the process. Staring the server on the command line works. Stacktrace: Traceback� The following are 40 code examples for showing how to use subprocess.call().These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

In case anyone else besides me doesn't see this in the (3.4) docs right away:

On Windows with shell=True, the COMSPEC environment variable specifies the default shell. 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 need shell=True to run a batch file or console-based executable.

Note Read the Security Considerations section before using shell=True.

subprocess.run python error, Traceback (most recent call last): File "e:\Dropbox (Personal)\My Python\external\ externalprogs.py", line 2, in <module> subprocess.run(['ls'� subprocess.call() Run the command described by “args”. We can run the command line with the arguments passed as a list of strings (example 1) or by setting the shell argument to a True value (example 2) Note, the default value of the shell argument is False. Let’s look at two examples where we show the summary of disk usage using

use Shell= True, It's working for me.

command line, By default subprocess.call doesn't use a shell to run our commands you so can't shell "+ os.path.expanduser('~') + "/catkin_ws/src)"]) will expect to find binary that looks like The other way , is to call specific shell explicitly. If encoding or errors are specified, or text is true, file objects for stdin, stdout and stderr are opened in text mode using the specified encoding and errors or the io.TextIOWrapper default. The universal_newlines argument is equivalent to text and is provided for backwards compatibility. By default, file objects are opened in binary mode.

Python's os and subprocess Popen Commands, Python offers several options to run external processes and interact with the operating system. The output is an open file that can be accessed by other programs. Within this module, we find the new Popen class. use of Popen in advanced cases, when other methods such like subprocess.call cannot fulfill our needs. That way the output is stored into a file, using a buffer to temporarily hold information in memory until a large enough section is worth writing to the file. If encoding is specified (Python 3.6+), the incoming bytes will be decoded and the buffer will be treated as text, aka “text mode”. This can also happen if either errors or universal

17.1. subprocess — Subprocess management — Python 2.7.18 , See the warning under Frequently Used Arguments for details. Use Popen with the communicate() method when you need pipes. when searching the executable, so you can't specify the program's path relative to cwd.

17.5. subprocess — Subprocess management — Python 3.4.10 , For more advanced use cases, the underlying Popen interface can be used directly. If universal_newlines is False the file objects stdin, stdout and stderr will be If shell is True, the specified command will be executed through the shell. if the executed program cannot be found; the “b” replacements raise OSError�

Comments
  • dir is not an executable, it is a console-only command. Why not use os.listdir() instead?
  • strange, the code works for me on ubuntu, rhel, win xp, win 7
  • Actually I cannot call any executable on the path. The statement p = subprocess.call('dir', shell=True) works fine on another machine and I think that it is related. I wanted to simplify my question as much as possible so I only brought up the example with 'dir'. I updated the question...
  • Are you running the script from console or from some IDE?
  • Thanks for all the help so far. I appreciate it! I have updated my question with the outcome of your ideas.
  • Nice! This did it... The COMSPEC variable was set to %SystemRoot%\system32\cmd.exe;%SystemRoot%\system32\WindowsPowerShell\v1.0; Changed it to %SystemRoot%\system32\cmd.exe; and now it is working correctly. Thanks!
  • Interesting. I've never seen a COMSPEC set with multiple values—if that is indeed valid, it might be worth filing a bug against subprocess. Glad I could help you! 😊
  • The problem I had appeared after I had installed Windows Management Framework (microsoft.com/en-us/download/details.aspx?id=34595). I don't know the value of the COMSPEC variable before but afterwards it pointed to cmd.exe and powershell.exe. According to en.wikipedia.org/wiki/ComSpec the COMSPEC variable should only point to one path? Right? Maybe the install process of WMF does somethings strange or do you mean that subprocess should handle multiple paths in the COMSPEC variable?
  • I think that—particularly if Microsoft software changes COMSPEC in this manner (meaning it's valid)—subprocess should be able to deal with it, yeah. Wikipedia's knowledge would seem to be outdated.
  • For me , the values is already os.environ['COMSPEC'] 'C:\\Windows\\system32\\cmd.exe'..............and i am running this on jenkins.......Not working for me..........Digging more into it
  • Actually I want to call an executable, but that does not work either. I have updated my question...
  • it should probably be an edit on the accepted answer