Python:如何重定向此输出? [英] Python: How do I redirect this output?

查看:29
本文介绍了Python:如何重定向此输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过子进程调用 rtmpdump 并尝试将其输出重定向到一个文件.问题是我根本无法重定向它.

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.

我首先尝试将 sys.stdout 设置为打开的文件.这适用于 ls,但不适用于 rtmpdump.我还尝试设置 sys.stderr 以确保它也不起作用.

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.

然后我尝试使用带有命令行参数的>>文件",但它似乎再次不起作用.

I tried then using a ">> file" with the command line argument but again it doesn't seem to work.

另外,出于某种原因,即使我使用 subprocess.call 而不是 subprocess.check_output,Eclipse 也会打印 rtmpdump 的输出,并且无需调用打印方法.这是黑魔法!

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!

有什么建议吗?

这是一些示例代码.

    # /!\ 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 是 python 对父级输出流的想法.

sys.stdout is the python's idea of the parent's output stream.

无论如何你想改变孩子的输出流.

In any case you want to change the child's output stream.

subprocess.call 和 subprocess.Popen 为输出流采用命名参数.

subprocess.call and subprocess.Popen take named parameters for the output streams.

所以打开你想要输出的文件,然后将它作为适当的参数传递给子进程.

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)

您关于使用 >> 的讨论表明您正在使用 shell=True,或者认为您正在将参数传递给 shell.在任何情况下,最好使用子进程的数组形式,这样可以避免不必要的进程,以及 shell 的任何奇怪之处.

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.

所以我下载了 RTMPDump 并试用了它,看起来消息出现在 stderr 上.

So I downloaded RTMPDump and tried it out, it would appear the messages are appearing on stderr.

因此,对于以下程序,程序输出中没有任何内容,并且 rtmpdump 会记录到 stderr.txt 文件中:

So with the following program, nothing appears on the programs output, and the rtmpdump logs when into the stderr.txt file:

#!/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)

这篇关于Python:如何重定向此输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆