简体   繁体   English

在python 3中使用子进程

[英]Using subprocess in python 3

I was using the subprocess module to run the shell command in python 3. 我正在使用子进程模块在python 3中运行shell命令。
Here is my code 这是我的代码

import subprocess
filename = "somename.py"  # in practical i'm using a real file, this is just for example
subprocess.call("pep8 %s" % filename, shell=True)) 

The output for different files is just 0 or 1 . 不同文件的输出仅为01 I am quite new to python 3. Using this in 2.7 gives me the desired output, but here i am not able to figure it out. 我是python 3的新手,在2.7中使用它可以提供所需的输出,但是在这里我无法弄清楚。
This is the output i get in python 2.7 (for a file named - anu.py ) - 这是我在python 2.7中获得的输出(对于名为anu.py的文件)-

anu.py:2:1: W191 indentation contains tabs
anu.py:3:1: W191 indentation contains tabs
anu.py:3:7: E228 missing whitespace around modulo operator
anu.py:4:1: W191 indentation contains tabs
anu.py:5:1: W191 indentation contains tabs
anu.py:6:1: W191 indentation contains tabs
anu.py:7:1: W191 indentation contains tabs
anu.py:7:9: E231 missing whitespace after ','
anu.py:8:1: W191 indentation contains tabs
anu.py:9:1: W191 indentation contains tabs
1

Please help me guys. 请帮助我。 Thanks 谢谢

Update: 更新:
I tried using subprocess.check_output method, 我尝试使用subprocess.check_output方法,
Here is the ouput i got, 这是我得到的输出,

>>> subprocess.check_output(["pep8", "anu.py"])
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "X/subprocess.py", line 584, in check_output
it too will be used internally.  Example:
subprocess.CalledProcessError: Command '['pep8', 'anu.py']' returned non-zero exit status 1

subprocess.call only returns the exit code of the process it ran. subprocess.call仅返回其运行的进程的退出代码。 Normally I'd recommend using subprocess.check_output , which will return the actual output of the subprocess. 通常,我建议使用subprocess.check_output ,它将返回subprocess.check_output的实际输出。 However, in your particular case, pep8 will return a non-zero exit code in some cases, which will make check_output raise an exception. 但是,在特定情况下, pep8在某些情况下将返回非零退出代码,这将使check_output引发异常。 You can catch the exception and extract the output attribute from it: 您可以捕获异常并从中提取输出属性:

try:
    output = subprocess.check_output(['pep8', 'anu.py'])
except subprocess.CalledProcessError as e:
    output = e.output

Or just use subprocess.Popen directly: 或者直接使用subprocess.Popen

p = subprocess.Popen(['pep8', 'anu.py'], stdout=subprocess.PIPE)
(output, _) = p.communicate()

Note that the call behavior hasn't changed between Python 2.x and Python 3.x. 请注意,在Python 2.x和Python 3.x之间, call行为没有改变。 The difference in behavior you're seeing might be because you're running Python 2.7 in an interactive prompt, but running the Python 3 version as an actual script. 您所看到的行为差异可能是因为您正在交互式提示中运行Python 2.7,但是将Python 3版本作为实际脚本运行。 Using subprocess.call in the interactive prompt will still end up printing the output of the call, even though it's not actually being returned by the function. 在交互式提示中使用subprocess.call仍将最终打印该调用的输出,即使该函数并未实际返回该输出。

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

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