我想在Python脚本中访问apt-file update
命令的输出。 不幸的是,我无法访问输出。 我的尝试:
# Does not work: popen_ret = subprocess.Popen([r'apt-file', 'update']), stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output = popen_ret.communicate()[0] print output # Without the "update" argument, it does work: popen_ret = subprocess.Popen([r'apt-file'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output = popen_ret.communicate()[0] print output
我的猜测是,apt-file本身的问题是,即使直接从控制台,这创build一个空的文件:
apt-file update > delme.txt
然而
apt-file > delme.txt
工作正常。
其他peopele似乎也有同样的问题(但没有解决scheme): 在http://www.linuxquestions.org提出的问题
apt-file
是一个调用另一个Perl脚本diffindex-download
perl脚本。
当apt-file update
运行时产生的所有输出都是由diffindex-download
生成的。
我不是一个Perl专家,但显然这个脚本写入/dev/tty
而不是stdout
。 你可以阅读这里的区别: 写入到STDOUT和打开到“/ dev / tty”的文件句柄之间有什么区别?
所以,如果你想重定向这个脚本的输出,你应该这样做
script -c "apt-file update" output.log