Python Forum
[subprocess] Why stdout sent to stderr?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[subprocess] Why stdout sent to stderr?
#1
Hello,

Apparently, subprocess is currently the solution du jour to run an external application from Python 3.12.

For some reason, output is returned through stderr instead of stdout:

import subprocess
import sys
import os

os.chdir(r'c:\temp')

p = run( [ r'c:\ffprobe.exe','-hide_banner',r"input.mp4"], shell=True,capture_output = True,text = True, check=True)
#!!! outputs to stderr!!!
print( 'stdout:', p.stdout) #nothing
print( 'stderr:', p.stderr)
What am I doing wrong?

Thank you.
Reply
#2
I would be surprised if Python knows anything about run. However subprocess.run is another story.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
This causes your program to crash because run is not defined (as perfingo mentioned).
p = run( [ r'c:\ffprobe.exe','-hide_banner',r"input.mp4"], shell=True,capture_output = True,text = True, check=True)
Which means this is not running:
print( 'stderr:', p.stderr)
And the "stderr" you are seeing is the normal Python error handling. for your program, not from running a subprocess that calls ffprobe.

Why are you using shell=True?
Reply
#4
it's not uncommon for some programs to use stderr(as ffprobe do) for informational messages or even regular output,
particularly if the stdout is used for different kinds of data.
So use it like this.
import subprocess

output = subprocess.run(['ffprobe', '-hide_banner', 'sample.mp4'],capture_output=True, text='utf-8', universal_newlines=True)
print(output.stderr)
Output:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'sample.mp4': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: mp42mp41isomavc1 creation_time : 2015-08-07T09:13:02.000000Z Duration: 00:00:30.53, start: 0.000000, bitrate: 411 kb/s Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 480x270 [SAR 1:1 DAR 16:9], 301 kb/s, 30 fps, 30 tbr, 30 tbn, 60 tbc (default) Metadata: creation_time : 2015-08-07T09:13:02.000000Z handler_name : L-SMASH Video Handler encoder : AVC Coding Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 112 kb/s (default) Metadata: creation_time : 2015-08-07T09:13:02.000000Z handler_name : L-SMASH Audio Handler
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Performance options for sys.stdout.writelines dgrunwal 11 3,152 Aug-23-2022, 10:32 PM
Last Post: Pedroski55
  Did subprocess.Popen() causes main routine to pause stdout? liudr 4 3,666 May-04-2021, 08:58 PM
Last Post: liudr
  stderr redirect to file fmr300 2 3,597 Apr-03-2021, 01:31 AM
Last Post: fmr300
  changing stdout and stderr Skaperen 4 2,704 Dec-02-2020, 08:58 PM
Last Post: Skaperen
  Get stdout of a running process yok0 0 3,037 Aug-20-2020, 10:12 AM
Last Post: yok0
  connot write to sys.stderr anne 7 2,883 Jul-31-2020, 12:39 AM
Last Post: millpond
  how to output to stderr from inside a generator? Skaperen 2 1,841 Mar-03-2020, 03:17 AM
Last Post: Skaperen
  will with sys.stdout as f: close sys.stdout? Skaperen 9 4,629 Nov-03-2019, 07:57 AM
Last Post: Gribouillis
  Add stdout to text file maxtimbo 3 3,165 Feb-05-2019, 12:53 AM
Last Post: maxtimbo
  stdout buffering Skaperen 5 4,730 Jun-12-2018, 06:14 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020