如何重定向 apt-file update 输出

How to redirect apt-file update output

我想在 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 本身,因为即使直接从控制台,这也会创建一个空文件:

apt-file update > delme.txt

apt-file > delme.txt

工作正常。

其他人好像遇到了同样的问题(但没有解决办法): question posed at http://www.linuxquestions.org

apt-file 是调用另一个 perl 脚本的 perl 脚本 diffindex-download.

apt-file update为运行时的所有输出由diffindex-download生成。

我不是 perl 专家,但显然这个脚本写入 /dev/tty 而不是 stdout。您可以在此处了解差异: What is the difference between writing to STDOUT and a filehandle opened to "/dev/tty"?

所以如果你想重定向这个脚本的输出你应该做

script -c "apt-file update" output.log