2016-09-01 8 views
0

私はしばらく検索してもそれを理解できません... ここで私のコードが間違っています。python:Popen FileNotFoundErrorの問題:[WinError 2]

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) 

コマンドプロンプトが表示されます。

['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] 

ファイルパスに2つのバックスラッシュが間違っているようです。

私は

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

をすればそれは私がファイルがあると確信している

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

を返すでしょうね。 これをどのようにデバッグできますか?

+0

ダブルバックスラッシュが問題にならないように、最初のバックスラッシュが必要です –

+0

'shell = True'も渡すことができますか? – Idos

+0

パスが正しいです。ダブルバックスラッシュは問題ではなく、文字列の 'repr'のアーティファクトです。いずれにせよ、問題は最後の引数であり、 'start'ではないと確信していますか? 'start' a *コマンド*またはシェル組み込みですか? – Bakuriu

答えて

0

Popenは、実行するファイルの代わりにstartというファイルを検索しようとしているため、この問題が発生しています。 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> 

これが正常に動作します。この代わりに

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

使用::私は、リストの末尾にパスを入れた場合でも、

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) 

は、あなたは、彼らがすることを意図されている順序に依存しすぎて周りcmd_args.append('start ')cmd_args.append('/wait ')を交換する必要がある場合があります

関連する問題