Python: How to redirect this output?

I call rtmpdump through a subprocess and try to redirect its output to a file. The problem is that I just cannot redirect it.

I tried first setting up sys.stdout to an open file. This works, say, ls, but not for rtmpdump. I also tried installing sys.stderr just to make sure, and it also didn't work.

I tried using "→ file" with a command line argument, but again it does not work.

Also for writing, for some reason, Eclipse prints rtmpdump output, even if I use subprocess.call instead of subprocess.check_output and without calling the print method. This is black magic!

Any suggestions?

Edit: Here is a 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) 
+7
python subprocess
source share
2 answers

sys.stdout is a python idea of ​​a parent output stream.

In any case, you want to change the output stream of the children.

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

So, open the file you want to output, and then pass this as the appropriate argument to the subprocess.

 f = open("outputFile","wb") subprocess.call(argsArray,stdout=f) 

Your talk about using >> suggests that you use shell = True or think you are passing your arguments to the shell. In any case, it is better to use the form of the subprocess array, which avoids the unnecessary process and any oddities from the shell.

EDIT:

So, I downloaded RTMPDump and tried it, a message would appear that appears on stderr.

So, with the following program, nothing appears on the output of the programs, and the rtmpdump log is written to 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) 
+16
source share

See link to get output from subprocess on SO

  • Getting all output from .Popen subprocess
  • stack overflow

I assume that the path would be to collect the output and write it to a file directly, or provide file descriptors that you can write to.

Something like that:

 f = open('dump.txt', 'wb') p = subprocess.Popen(args, stdout=f, stderr=subprocess.STDOUT, shell=True) 
+1
source share

All Articles