简体   繁体   English

我可以让 subprocess.call 将调用的输出写入字符串吗?

[英]Can I have subprocess.call write the output of the call to a string?

I want to do subprocess.call, and get the output of the call into a string.我想做 subprocess.call,并将调用的输出转换为字符串。 Can I do this directly, or do I need to pipe it to a file, and then read from it?我可以直接执行此操作,还是需要将其通过管道传输到文件,然后从中读取?

In other words, can I somehow redirect stdout and stderr into a string?换句话说,我可以以某种方式将 stdout 和 stderr 重定向到字符串中吗?

No, you can't read the output of subprocess.call() directly into a string. 不,您无法将subprocess.call()的输出直接读入字符串。

In order to read the output of a command into a string, you need to use subprocess.Popen(), eg: 为了将命令的输出读入字符串,您需要使用subprocess.Popen(),例如:

>>> cmd = subprocess.Popen('ls', stdout=subprocess.PIPE)
>>> cmd_out, cmd_err = cmd.communicate()

cmd_out will have the string with the output of the command. cmd_out将包含该命令输出的字符串。

This is an extension of mantazer's answer to python3. 这是mantazer对python3的回答的扩展。 You can still use the subprocess.check_output command in python3: 您仍然可以在python3中使用subprocess.check_output命令:

>>> subprocess.check_output(["echo", "hello world"])
b'hello world\n'

however now it gives us a byte string. 但是现在它给了我们一个字节串。 To get a real python string we need to use decode: 要获得真正的python字符串,我们需要使用decode:

>>> subprocess.check_output(["echo", "hello world"]).decode(sys.stdout.encoding)
'hello world\n'

Using sys.stdout.encoding as the encoding rather than just the default UTF-8 should make this work on any OS (at least in theory). 使用sys.stdout.encoding作为编码而不仅仅是默认的UTF-8应该可以在任何操作系统上运行(至少在理论上)。

The trailing newline (and any other extra whitespace) can be easily removed using .strip() , so the final command is: 可以使用.strip()轻松删除尾随换行符(以及任何其他额外的空格.strip() ,因此最终命令为:

>>> subprocess.check_output(["echo", "hello world"]
                            ).decode(sys.stdout.encoding).strip()
'hello world'

The subprocess module provides a method check_output() that runs the provided command with arguments (if any), and returns its output as a byte string. subprocess check_output()模块提供了一个方法check_output() ,它使用参数(如果有)运行提供的命令,并将其输出作为字节字符串返回。

output = subprocess.check_output(["echo", "hello world"])
print output

The code above will print hello world 上面的代码将打印hello world

See docs: https://docs.python.org/2/library/subprocess.html#subprocess.check_output 请参阅docs: https//docs.python.org/2/library/subprocess.html#subprocess.check_output

subprocess.call() takes the same arguments as subprocess.Popen() , which includes the stdout and stderr arguments. subprocess.call()取相同的参数subprocess.Popen()其中包括stdoutstderr参数。 See the docs for details. 有关详细信息,请参阅文档。

Using text=True in Python 3.7+在 Python 3.7+ 中使用text=True

In newer versions of Python, you can simply use text=True to get a string return value:在较新版本的 Python 中,您可以简单地使用text=True来获取字符串返回值:

>>> import subprocess
>>> subprocess.check_output(["echo", "hello"], text=True)
'hello\n'

Here is what the docs say :这是文档所说的

If encoding or errors are specified, or text is true, file objects for stdin, stdout and stderr are opened in text mode using the specified encoding and errors or the io.TextIOWrapper default.如果指定了encodingerrors ,或者text为真,则使用指定的encodingerrorsio.TextIOWrapper默认值以文本模式打开 stdin、stdout 和 stderr 的文件对象。 The universal_newlines argument is equivalent to text and is provided for backwards compatibility. universal_newlines参数等效于text并提供向后兼容性。 By default, file objects are opened in binary mode.默认情况下,文件对象以二进制模式打开。

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

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