Python:如何重定向此输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3982577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: How do I redirect this output?
提问by dmc
I'm calling rtmpdump via subprocess and trying to redirect its output to a file. The problem is that I simply can't redirect it.
我正在通过子进程调用 rtmpdump 并尝试将其输出重定向到一个文件。问题是我根本无法重定向它。
I tried first setting up the sys.stdout to the opened file. This works for, say, ls, but not for rtmpdump. I also tried setting the sys.stderr just to make sure and it also didn't work.
我首先尝试将 sys.stdout 设置为打开的文件。这适用于 ls,但不适用于 rtmpdump。我还尝试设置 sys.stderr 以确保它也不起作用。
I tried then using a ">> file" with the command line argument but again it doesn't seem to work.
然后我尝试使用带有命令行参数的“>>文件”,但它似乎再次不起作用。
Also for the record, for some reason, Eclipse prints rtmpdump's output even if I use subprocess.call instead of subprocess.check_output, and without having to call the print method. This is black magic!
同样作为记录,出于某种原因,即使我使用 subprocess.call 而不是 subprocess.check_output,Eclipse 也会打印 rtmpdump 的输出,并且无需调用打印方法。这是黑魔法!
Any suggestions?
有什么建议?
Edit: Here's some sample code.
编辑:这是一些示例代码。
# /!\ note: need to use os.chdir first to get to the folder with rtmpdump!
command = './rtmpdump -r rtmp://oxy.videolectures.net/video/ -y 2007/pascal/bootcamp07_vilanova/keller_mikaela/bootcamp07_keller_bss_01 -a video -s http://media.videolectures.net/jw-player/player.swf -w ffa4f0c469cfbe1f449ec42462e8c3ba16600f5a4b311980bb626893ca81f388 -x 53910 -o test.flv'
split_command = shlex.split(command)
subprocess.call(split_command)
采纳答案by Douglas Leeder
sys.stdout is the python's idea of the parent's output stream.
sys.stdout 是 python 的父输出流的想法。
In any case you want to change the child's output stream.
在任何情况下,您都想更改孩子的输出流。
subprocess.call and subprocess.Popen take named parameters for the output streams.
subprocess.call 和 subprocess.Popen 为输出流采用命名参数。
So open the file you want to output to and then pass that as the appropriate argument to subprocess.
因此,打开要输出到的文件,然后将其作为适当的参数传递给 subprocess。
f = open("outputFile","wb")
subprocess.call(argsArray,stdout=f)
Your talk of using >>suggest you are using shell=True, or think you are passing your arguments to the shell. In any case it is better to use the array form of subprocess, which avoid an unnecessary process, and any weirdness from the shell.
您对 using 的讨论>>表明您正在使用 shell=True,或者认为您正在将参数传递给 shell。在任何情况下,最好使用子进程的数组形式,这样可以避免不必要的进程,以及 shell 的任何奇怪之处。
EDIT:
编辑:
So I downloaded RTMPDump and tried it out, it would appear the messages are appearing on stderr.
所以我下载了 RTMPDump 并尝试了一下,看起来消息出现在 stderr 上。
So with the following program, nothing appears on the programs output, and the rtmpdump logs when into the stderr.txt file:
因此,对于以下程序,程序输出中没有任何内容,并且 rtmpdump 会记录到 stderr.txt 文件中:
#!/usr/bin/env python
import os
import subprocess
RTMPDUMP="./rtmpdump"
assert os.path.isfile(RTMPDUMP)
command = [RTMPDUMP,'-r','rtmp://oxy.videolectures.net/video/',
'-y','2007/pascal/bootcamp07_vilanova/keller_mikaela/bootcamp07_keller_bss_01',
'-a','video','-s',
'http://media.videolectures.net/jw-player/player.swf',
'-w','ffa4f0c469cfbe1f449ec42462e8c3ba16600f5a4b311980bb626893ca81f388'
,'-x','53910','-o','test.flv']
stdout = open("stdout.txt","wb")
stderr = open("stderr.txt","wb")
subprocess.call(command,stdout=stdout,stderr=stderr)
回答by pyfunc
See the link on getting the output from subprocess on SO
请参阅有关从 SO 上的子进程获取输出的链接
- Getting the entire output from subprocess.Popen
- https://stackoverflow.com/questions/tagged/subprocess
I guess the way would be to collect the output and write it to a file directly or provide file descriptors to which you output can be written.
我想方法是收集输出并将其直接写入文件或提供可以写入输出的文件描述符。
Something like this:
像这样的东西:
f = open('dump.txt', 'wb')
p = subprocess.Popen(args, stdout=f, stderr=subprocess.STDOUT, shell=True)

