简体   繁体   English

Python:如何重定向此输出?

[英]Python: How do I redirect this output?

I'm calling rtmpdump via subprocess and trying to redirect its output to a file. 我通过子进程调用rtmpdump并尝试将其输出重定向到文件。 The problem is that I simply can't redirect it. 问题是我根本无法重定向它。

I tried first setting up the sys.stdout to the opened file. 我尝试首先将sys.stdout设置为打开的文件。 This works for, say, ls, but not for rtmpdump. 这适用于ls,但不适用于rtmpdump。 I also tried setting the sys.stderr just to make sure and it also didn't work. 我也试过设置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. 同样为了记录,由于某种原因,Eclipse打印rtmpdump的输出,即使我使用subprocess.call而不是subprocess.check_output,也无需调用print方法。 This is black magic! 这是黑魔法!

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)

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. 因此,打开要输出的文件,然后将其作为适当的参数传递给子进程。

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. 你谈论使用>>建议你使用shell = True,或者认为你正在将你的参数传递给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. 在任何情况下,最好使用子进程的数组形式,这避免了不必要的进程,以及来自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)

See the link on getting the output from subprocess on SO 请参阅有关从SO获取子进程输出的链接

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM