Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: trouble with Popen FileNotFoundError: [WinError 2]

Tags:

python

I've search a while and still can not figure it out... Here's part of my code that went wrong.

import subprocess as sp
import os
cmd_args = []
cmd_args.append('start ')
cmd_args.append('/wait ')
cmd_args.append(os.path.join(dirpath,filename))
print(cmd_args)
child = sp.Popen(cmd_args)

And the command prompt through out this.

['start ', '/wait ', 'C:\\Users\\xxx\\Desktop\\directory\\myexecutable.EXE']
Traceback (most recent call last):
  File "InstallALL.py", line 89, in <module>
    child = sp.Popen(cmd_args)
  File "C:\Python34\lib\subprocess.py", line 859, in __init__
    restore_signals, start_new_session)
  File "C:\Python34\lib\subprocess.py", line 1114, in _execute_child startupinfo)
FileNotFoundError: [WinError 2]

It looks like the filepath is wrong with 2 backslashes.

I know if I do

print(os.path.join(dirpath,filename))

It'll return

C:\Users\xxx\Desktop\directory\myexecutable.EXE

I'm sure the file is there. How can I debug this?

like image 729
user2869934 Avatar asked Sep 01 '16 10:09

user2869934


People also ask

How do I use subprocess Popen in Python?

The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle. For more advanced use cases, the underlying Popen interface can be used directly. Run the command described by args. Wait for command to complete, then return a CompletedProcess instance.

What is Popen in Python?

Python method popen() opens a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'. The bufsize argument has the same meaning as in open() function.

Does subprocess Popen need to be closed?

There the connection is not closed. So you do not need to close most probably. unrelated: you could use stdin=open('test. sql', 'rb', 0) to redirect child's stdin from the file without loading the whole file into your Python process first.

What is Shell true in subprocess Python?

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.


1 Answers

This is happening because Popen is trying to find the file start instead of the file you want to run.

For example, using notepad.exe:

>>> import subprocess
>>> subprocess.Popen(['C:\\Windows\\System32\\notepad.exe', '/A', 'randomfile.txt']) # '/A' is a command line option
<subprocess.Popen object at 0x03970810>

This works fine. But if I put the path at the end of the list:

>>> subprocess.Popen(['/A', 'randomfile.txt', 'C:\\Windows\\System32\\notepad.exe'])
Traceback (most recent call last):
  File "<pyshell#53>", line 1, in <module>
    subprocess.Popen(['/A', 'randomfile.txt', 'C:\\Windows\\System32\\notepad.exe'])
  File "C:\python35\lib\subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "C:\python35\lib\subprocess.py", line 1220, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

Use this instead:

import subprocess as sp
import os
cmd_args = []
cmd_args.append(os.path.join(dirpath,filename))
cmd_args.append('start ')
cmd_args.append('/wait ')
print(cmd_args)
child = sp.Popen(cmd_args)

You might need to swap cmd_args.append('start ') and cmd_args.append('/wait ') around too depending on which order they are meant to be in.

like image 177
Farhan.K Avatar answered Oct 23 '22 03:10

Farhan.K