在python 3中使用子进程 [英] Using subprocess in python 3

查看:53
本文介绍了在python 3中使用子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 subprocess 模块在 python 3 中运行 shell 命令.
这是我的代码

导入子流程filename = "somename.py" # 实际上我使用的是真实文件,这只是举例subprocess.call("pep8 %s" % 文件名,shell=True))

不同文件的输出只是01.我对 python 3 很陌生.在 2.7 中使用它给了我所需的输出,但在这里我无法弄清楚.
这是我在 python 2.7 中得到的输出(对于名为 - anu.py 的文件) -

anu.py:2:1: W191 缩进包含制表符anu.py:3:1: W191 缩进包含制表符anu.py:3:7: E228 缺少模运算符周围的空格anu.py:4:1: W191 缩进包含制表符anu.py:5:1: W191 缩进包含制表符anu.py:6:1: W191 缩进包含制表符anu.py:7:1: W191 缩进包含制表符anu.py:7:9: E231 ',' 后缺少空格anu.py:8:1: W191 缩进包含制表符anu.py:9:1: W191 缩进包含制表符1

请帮帮我.谢谢

更新:
我尝试使用 subprocess.check_output 方法,
这是我得到的输出,

<预><代码>>>>subprocess.check_output(["pep8", "anu.py"])回溯(最近一次调用最后一次):文件<string>",第 1 行,在 <module> 中check_output 中的文件X/subprocess.py",第 584 行它也将在内部使用.例子:subprocess.CalledProcessError: 命令 '['pep8', 'anu.py']' 返回非零退出状态 1

解决方案

subprocess.call 只返回它运行的进程的退出代码.通常我会建议使用 subprocess.check_output,它将返回子进程的实际输出.但是,在您的特定情况下, pep8 在某些情况下将返回非零退出代码,这将使 check_output 引发异常.您可以捕获异常并从中提取输出属性:

尝试:output = subprocess.check_output(['pep8', 'anu.py'])除了 subprocess.CalledProcessError 为 e:输出 = e.output

或者直接使用subprocess.Popen:

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

请注意,call 行为在 Python 2.x 和 Python 3.x 之间没有改变.您所看到的行为差异可能是因为您在交互式提示中运行 Python 2.7,但将 Python 3 版本作为实际脚本运行.在交互式提示中使用 subprocess.call 仍然会打印调用的输出,即使它实际上没有被函数返回.

I was using the subprocess module to run the shell command in python 3.
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. 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.
This is the output i get in python 2.7 (for a file named - 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,
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. Normally I'd recommend using subprocess.check_output, which will return the actual output of the subprocess. However, in your particular case, pep8 will return a non-zero exit code in some cases, which will make check_output raise an exception. 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:

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. 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. 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.

这篇关于在python 3中使用子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆