【发布时间】:2014-08-24 18:32:25
【问题描述】:
我想知道你是否可以帮我解决一个问题:
在 python 中,我一直试图将 '>' 赋予 subprocess.Popen([]) 或 subprocess.call([]),但不知何故它改变了它在终端中的输入方式。一个例子。
终端命令:
iperf -s -u -y C > data.csv
Python 代码:
import subprocess as sub
sub.Popen(['iperf', '-s', '-u', '-y', 'C', '>', 'data.csv'])
或
sub.Popen(['{iperf', '-s', '-u', '-y', 'C}', '>', 'data.csv'])
当我在终端运行第一个命令时,它会执行得很好,但是当我执行第二个命令时,它会忽略'>'和'data.csv':
$ python test.py
iperf: ignoring extra argument -- >
iperf: ignoring extra argument -- data.csv
第三个命令返回:
$ python test.py
Traceback (most recent call last):
File "test.py", line 3, in <module>
sub.call(['{iperf', '-s', '-u', '-y', 'C}', '>', 'data.csv'])
File "/usr/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
我已经尝试在 DuckDuckGo 和 Google 上搜索这个问题,但我找不到答案,因为他们不会解释“>”符号,即使用作“>”也是如此。
期待您的回答,非常感谢!
【问题讨论】: