你如何在 Python 中使用 subprocess.check_output()? [英] How do you use subprocess.check_output() in Python?

查看:65
本文介绍了你如何在 Python 中使用 subprocess.check_output()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了关于 subprocess.check_output() 的文档,但我找不到带参数的文档,而且文档不是很深入.我正在使用 Python 3(但我正在尝试通过 Python 3 运行 Python 2 文件)

我正在尝试运行此命令:python py2.py -i test.txt

-i 是 argparse 的位置参数,test.txt 是 -i 是什么,py2.py 是要运行的文件

我尝试了很多(非工作)变体,包括:py2output = subprocess.check_output([str('python py2.py'),'-i','test.txt'])

py2output = subprocess.check_output([str('python'),'py2.py','-i', test.txt'])

解决方案

正确答案(使用 Python 2.7 及更高版本,因为 check_output() 当时引入) 是:

py2output = subprocess.check_output(['python','py2.py','-i','test.txt'])

为了演示,这里是我的两个程序:

py2.py:

导入系统打印 sys.argv

py3.py:

导入子流程py2output = subprocess.check_output(['python', 'py2.py', '-i', 'test.txt'])print('py2 说:', py2output)

运行:

$ python3 py3.pypy2 说:b"['py2.py', '-i', 'test.txt']\n"

您的每个版本都有哪些问题:

py2output = subprocess.check_output([str('python py2.py'),'-i','test.txt'])

首先,str('python py2.py')'python py2.py' 完全一样——你正在使用 str,并调用 str 将其转换为 str.这使得代码更难阅读、更长,甚至更慢,而且没有任何好处.

更严重的是,python py2.py 不能是单个参数,除非您实际上是在尝试运行一个名为 /usr/bin/python\ py2 的程序.py.你不是;例如,您正在尝试使用第一个参数 py2.py 运行 /usr/bin/python.所以,你需要让它们在列表中分开元素.

您的第二个版本修复了这个问题,但是您在 test.txt' 之前缺少了 '.这应该会给你一个 SyntaxError,可能是说 EOL while scan string literal.

与此同时,我不确定您是如何找到文档的,但找不到任何带有参数的示例.第一个例子是:

<预><代码>>>>subprocess.check_output(["echo", "Hello World!"])b'世界你好!\n'

这会调用带有附加参数 "Hello World!""echo" 命令.

还有:

<块引用>

-i 是 argparse 的位置参数,test.txt 是 -i 是什么

我很确定-i不是一个位置参数,而是一个可选参数.否则,后半句就没有意义了.

I have found documentation about subprocess.check_output() but I cannot find one with arguments and the documentation is not very in depth. I am using Python 3 (but am trying to run a Python 2 file through Python 3)

I am trying to run this command: python py2.py -i test.txt

-i is a positional argument for argparse, test.txt is what the -i is, py2.py is the file to run

I have tried a lot of (non working) variations including: py2output = subprocess.check_output([str('python py2.py '),'-i', 'test.txt'])

py2output = subprocess.check_output([str('python'),'py2.py','-i', test.txt'])

解决方案

The right answer (using Python 2.7 and later, since check_output() was introduced then) is:

py2output = subprocess.check_output(['python','py2.py','-i', 'test.txt'])

To demonstrate, here are my two programs:

py2.py:

import sys
print sys.argv

py3.py:

import subprocess
py2output = subprocess.check_output(['python', 'py2.py', '-i', 'test.txt'])
print('py2 said:', py2output)

Running it:

$ python3 py3.py
py2 said: b"['py2.py', '-i', 'test.txt']\n"

Here's what's wrong with each of your versions:

py2output = subprocess.check_output([str('python py2.py '),'-i', 'test.txt'])

First, str('python py2.py') is exactly the same thing as 'python py2.py'—you're taking a str, and calling str to convert it to an str. This makes the code harder to read, longer, and even slower, without adding any benefit.

More seriously, python py2.py can't be a single argument, unless you're actually trying to run a program named, say, /usr/bin/python\ py2.py. Which you're not; you're trying to run, say, /usr/bin/python with first argument py2.py. So, you need to make them separate elements in the list.

Your second version fixes that, but you're missing the ' before test.txt'. This should give you a SyntaxError, probably saying EOL while scanning string literal.

Meanwhile, I'm not sure how you found documentation but couldn't find any examples with arguments. The very first example is:

>>> subprocess.check_output(["echo", "Hello World!"])
b'Hello World!\n'

That calls the "echo" command with an additional argument, "Hello World!".

Also:

-i is a positional argument for argparse, test.txt is what the -i is

I'm pretty sure -i is not a positional argument, but an optional argument. Otherwise, the second half of the sentence makes no sense.

这篇关于你如何在 Python 中使用 subprocess.check_output()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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