2016-11-16 44 views
2

代碼:的Python:subprocess.Popen - 沒有這樣的文件或目錄

#!/usr/bin/env python 
import subprocess 

cmd="ls -lrt" 
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) 
(stdoutdata, stderrdata) = process.communicate() 
print stdoutdata 

錯誤:

Traceback (most recent call last): 
    File "test.py", line 5, in <module> 
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) 
    File "/usr/lib64/python2.6/subprocess.py", line 642, in __init__ 
    errread, errwrite) 
    File "/usr/lib64/python2.6/subprocess.py", line 1238, in _execute_child 
    raise child_exception 
OSError: [Errno 2] No such file or directory 

可有人請指教一下是與上面的代碼的問題?這是在process=subprocess.Popen聲明失敗。

+1

'Popen'預計列表'[ 「LS」, 「-lrt」]'(如果你不使用'殼= True') - 檢查文檔。 – furas

回答

3

重寫代碼如下

#!/usr/bin/env python 
import subprocess 

cmd=["ls","-lrt"] 
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) 
(stdoutdata, stderrdata) = process.communicate() 
print stdoutdata 

命令應該是一個列表

相關問題