2016-04-29 59 views
0

Python的空闲时,显示当我试图使用WinRAR解压缩(UnRAR.exe)文件的错误:subprocess.CalledProcessError使用Python中的unrar

"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) 
+1

当您从命令行调用Unrar时,是否使用完全相同的参数调用Unrar? –

+0

我把我的钱放在NO。 – 7stud

+0

是的!它在cmd中工作得很好 –

回答

1

我会对此发表评论,但我没有足够的声望来这样做。

尝试没有shell界面运行的命令,就是

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

我也删除通过使用三引号从您的命令添加额外的反斜线的复杂性。这几乎更精确,因为您确切知道正在运行的命令和参数。

也可以在Windows = True时,不需要外壳,除非你正在运行内置的命令外壳,https://docs.python.org/3/library/subprocess.html#popen-constructor

在Windows与壳牌= TRUE,则COMSPEC环境变量指定默认的shell。唯一需要在Windows上指定shell = True的时间是当您希望执行的命令构建到shell中时(例如dir或copy)。您不需要shell = True来运行批处理文件或基于控制台的可执行文件。

+0

代码实际上是与用户权限有关的问题,因为代码正试图提取\ system32 \文件夹中的文件。 但是,感谢大家花费宝贵的时间回答这个问题 –