簡體   English   中英

subprocess.CalledProcessError在使用unrar的python中

[英]subprocess.CalledProcessError In python when using unrar

當我嘗試使用Winrar(UnRAR.exe)提取文件時,Python IDLE顯示錯誤:

"Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    response=subprocess.check_output(['"C:\\Users\\B74Z3\\Desktop\\Test\\UnRAR.exe" e -p123 "C:\\Users\\B74Z3\\Desktop\\Test\\Test.rar"'], shell=True)
  File "C:\Program Files\Python 3.5\lib\subprocess.py", line 629, in check_output
    **kwargs).stdout
  File "C:\Program Files\Python 3.5\lib\subprocess.py", line 711, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['"C:\\Users\\B74Z3\\Desktop\\Test\\UnRAR.exe" e -p123 "C:\\Users\\B74Z3\\Desktop\\Test\\Test.rar"']' returned non-zero exit status 1"

代碼有什么問題:

import subprocess
response=subprocess.check_output(['"C:\\Users\\B74Z3\\Desktop\\Test\\UnRAR.exe" e -p123 "C:\\Users\\B74Z3\\Desktop\\Test\\Test.rar"'], shell=True)

我對此發表評論,但是我沒有足夠的聲譽。

嘗試在沒有shell界面的情況下運行命令,即

response=subprocess.check_output(["""C:\Users\B74Z3\Desktop\Test\UnRAR.exe""", "e", "-p123', """C:\Users\B74Z3\Desktop\Test\Test.rar"""])

我還通過使用三重引號消除了在命令中添加其他反斜杠的復雜性。 因為您確切地知道正在運行的命令和參數,所以這幾乎更為精確。

同樣在Windows上,除非您運行的是內置命令https://docs.python.org/3/library/subprocess.html#popen-constructor ,否則不需要shell = True:

在shell = True的Windows上,COMSPEC環境變量指定默認的Shell。 在Windows上唯一需要指定shell = True的時間是將要執行的命令內置到shell中(例如dir或copy)。 您不需要shell = True即可運行批處理文件或基於控制台的可執行文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM