How to Pipe a Subprocess Call to a Text File

How do I pipe a subprocess call to a text file?

If you want to write the output to a file you can use the stdout-argument of subprocess.call.

It takes either

  • None (the default, stdout is inherited from the parent (your script))
  • subprocess.PIPE (allows you to pipe from one command/process to another)
  • a file object or a file descriptor (what you want, to have the output written to a file)

You need to open a file with something like open and pass the object or file descriptor integer to call:

f = open("blah.txt", "w")
subprocess.call(["/home/myuser/run.sh", "/tmp/ad_xml", "/tmp/video_xml"], stdout=f)

I'm guessing any valid file-like object would work, like a socket (gasp :)), but I've never tried.

As marcog mentions in the comments you might want to redirect stderr as well, you can redirect this to the same location as stdout with stderr=subprocess.STDOUT. Any of the above mentioned values works as well, you can redirect to different places.

How to use subprocesses.call and pipe one output to a txt file?

The > is a shell redirection command but you didn't run through a shell. You can do what the shell would do: open the file and attach it as the program's stdout.

subprocess.call(['python2.7', 'cello_client.py', 'get_results',
'--jobid','pythonTest4', '--filename',
'pythonTest4_dnacompiler_output.txt'],
stdout=open('out.txt', 'wb'))

You can read stdout directly into memory with a different subprocess command:

proc = subprocess.Popen(['python2.7', 'cello_client.py', 'get_results',
'--jobid','pythonTest4', '--filename',
'pythonTest4_dnacompiler_output.txt'])
out_text, err_text = proc.communicate()
return_code = proc.returncode

Redirect subprocess.run output to file

Open the file in write (>) or append (>>) mode, and assign the descriptor associated with it to stdout in subprocess.run call.

with open('outputfile.txt', 'w') as fd:
subprocess.run(['grep', '-o', searchFor, filename], stdout=fd)

How do I redirect stdout to a file when using subprocess.call in python?

Pass a file as the stdout parameter to subprocess.call:

with open('out-file.txt', 'w') as f:
subprocess.call(['program'], stdout=f)

Piping output of subprocess.Popen to files

Per the docs,

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

So just pass the open-for-writing file objects as named arguments stdout= and stderr= and you should be fine!

Retrieving the output of subprocess.call()

Output from subprocess.call() should only be redirected to files.

You should use subprocess.Popen() instead. Then you can pass subprocess.PIPE for the stderr, stdout, and/or stdin parameters and read from the pipes by using the communicate() method:

from subprocess import Popen, PIPE

p = Popen(['program', 'arg1'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate(b"input data that is passed to subprocess' stdin")
rc = p.returncode

The reasoning is that the file-like object used by subprocess.call() must have a real file descriptor, and thus implement the fileno() method. Just using any file-like object won't do the trick.

See here for more info.

Store output of subprocess.Popen call in a string

In Python 2.7 or Python 3

Instead of making a Popen object directly, you can use the subprocess.check_output() function to store output of a command in a string:

from subprocess import check_output
out = check_output(["ntpq", "-p"])

In Python 2.4-2.6

Use the communicate method.

import subprocess
p = subprocess.Popen(["ntpq", "-p"], stdout=subprocess.PIPE)
out, err = p.communicate()

out is what you want.

Important note about the other answers

Note how I passed in the command. The "ntpq -p" example brings up another matter. Since Popen does not invoke the shell, you would use a list of the command and options—["ntpq", "-p"].



Related Topics



Leave a reply



Submit