2017-06-01 68 views
0

我是python腳本編程的新手。我試圖在Python空閒時運行子進程方法並獲取這些錯誤:使用空閒的Python子進程

import subprocess 
subprocess.check_output("ls") 

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "C:\Users\ramakrishna\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 336, in check_output 
    **kwargs).stdout 
File "C:\Users\ramakrishna\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 403, in run 
    with Popen(*popenargs, **kwargs) as process: 
File "C:\Users\ramakrishna\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 707, in __init__ 
    restore_signals, start_new_session) 
File "C:\Users\ramakrishna\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 990, in _execute_child 
    startupinfo) 
FileNotFoundError: [WinError 2] The system cannot find the file specified 

請幫我解決這個錯誤。 謝謝

回答

0

好像你正在運行的窗口。 Windows沒有「ls」命令,所以你得到一個FileNotFoundError異常。我不熟悉Windows和Python,但嘗試「dir」而不是「ls」,這應該工作。

+0

我相信'dir'也不是一個文件,所以我期望'shell = True'選項是需要的。 –

+0

是的,shell = True與'dir'正常工作。你能否建議我使用OS相關的任何材料?請告訴我! –