简体   繁体   English

subprocess.CalledProcessError在使用unrar的python中

[英]subprocess.CalledProcessError In python when using unrar

Python IDLE shows an error when I am trying to extract files using winrar(UnRAR.exe): 当我尝试使用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"

What is the problem with the Code: 代码有什么问题:

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

I'd comment on this, but I don't have enough reputation to do so. 我对此发表评论,但是我没有足够的声誉。

Try running the command without the shell interface, that is, 尝试在没有shell界面的情况下运行命令,即

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

I've also remove the complexity of adding additional backslashes from your command by using triple quotes. 我还通过使用三重引号消除了在命令中添加其他反斜杠的复杂性。 This is almost more precise in that you know exactly what command and arguments is being run. 因为您确切地知道正在运行的命令和参数,所以这几乎更为精确。

Also on windows the shell=True is not needed unless you're running a shell built in command, https://docs.python.org/3/library/subprocess.html#popen-constructor : 同样在Windows上,除非您运行的是内置命令https://docs.python.org/3/library/subprocess.html#popen-constructor ,否则不需要shell = True:

On Windows with shell=True, the COMSPEC environment variable specifies the default shell. 在shell = True的Windows上,COMSPEC环境变量指定默认的Shell。 The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (eg dir or copy). 在Windows上唯一需要指定shell = True的时间是将要执行的命令内置到shell中(例如dir或copy)。 You do not need shell=True to run a batch file or console-based executable. 您不需要shell = True即可运行批处理文件或基于控制台的可执行文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM