Skip to content Skip to sidebar Skip to footer

How To Use A Custom File-like Object As Subprocess Stdout/stderr?

Consider this code, where a subprocess.Popen is spawned. I'd like writes to the subprocess' stdout and stderr to go to my custom file-object's .write() method, however this isn't t

Solution 1:

According to the documentation:

stdin, stdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively. Valid values are PIPE, DEVNULL, an existing file descriptor (a positive integer), an existing file object, and None.

Using subprocess.PIPE:

proc = subprocess.Popen(['bash', '-c', 'echo Testing'], 
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT)
print('Writing:', proc.stdout.read())
# OR  print('Writing:', proc.stdout.read().decode())

Solution 2:

Not directly. Maybe some future version of Python will support converting file-like objects into some sort of auto-populating pipe, but the crux of it is that the subprocess needs to have access to a file handle it can read without calling Python code somehow. This means it needs to be something that works at an OS level, meaning it is limited to a few possibilities:

  • Inheriting stdin from the parent process (which happens when stdin=None)
  • Taking input from a file (which happens when stdin is a file or an integer file handle)
  • Taking input from a pipe (which happens when stdin=subprocess.PIPE)

Using a file-like object means you're going to have to read the data yourself and then feed it through a pipe.

For example:

proc = subprocess.Popen(['sha256sum', '-'], stdin=subprocess.PIPE)
while True:
    chunk = filelike.read(BLOCK_SIZE)
    proc.stdin.write(chunk)
    iflen(chunk) < BLOCK_SIZE:
        break
proc.wait()

Post a Comment for "How To Use A Custom File-like Object As Subprocess Stdout/stderr?"