Windows can't find the file on subprocess.call()

115,779

Solution 1

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 need shell=True to run a batch file or console-based executable.

Solution 2

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.)

Solution 3

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.

Solution 4

If you are using powershell, then in it will be subprocess.call(['powershell','-command','dir']). Powershell supports a large portion of POSIX commands

Solution 5

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/

Share:
115,779
Sri
Author by

Sri

Make stuff. Build things. - https://srirangan.net

Updated on July 21, 2022

Comments

  • Sri
    Sri almost 2 years

    I am getting the following error:

    WindowsError: [Error 2] The system cannot find the file specified
    

    My code is:

    subprocess.call(["<<executable file found in PATH>>"])
    

    Windows 7, 64 bit. Python 3.x latest, stable.

    Any ideas?

    Thanks,

  • Apalala
    Apalala over 13 years
    That's because there's no executable called dir.exe while there's a /bin/ls in *nix. dir is implemented by CMD.EXE much like cd is implemented by bash.
  • nu everest
    nu everest about 8 years
    This is strongly discouraged. docs.python.org/2/library/…
  • Jirka
    Jirka almost 8 years
    @nueverest Only when the the command string is constructed from external input
  • Moondra
    Moondra almost 7 years
    How would I keep the screen open?
  • asmeurer
    asmeurer almost 7 years
    The alternative (more secure for external input) is to get the PATH from the os.environ and search it manually.
  • Naramsim
    Naramsim about 6 years
    Any python 2 option?
  • User5910
    User5910 almost 6 years
    @Moondra, If I understand you correctly, try /k instead of /c. Enter cmd /? at the command line for details.
  • Allan Chain
    Allan Chain over 2 years
    @Naramsim Try from distutils.spawn import find_executable as which. This line is copied from pre-commit's hook script.
  • tripleee
    tripleee over 2 years
    This is good advice per se, but does nothing to solve the actual question here.
  • enjoi4life411
    enjoi4life411 over 2 years
    This answer helped me when getting WinError 5 permission denined on Windows 10 using powershell