Questions tagged [subprocess]

The Python subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. Use it to run a shell command or an executable in Python.

The Python subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.

If you want to run Python code in a separate process consider instead.

is preferable to subprocess in some cases.

Waiting for a command to finish and getting the result

Interacting with a subprocess while it is still running

Windows

Misc

12158 questions
6018
votes
66 answers

How do I execute a program or call a system command?

How do I call an external command within Python as if I had typed it in a shell or command prompt?
freshWoWer
  • 61,969
  • 10
  • 36
  • 35
1381
votes
25 answers

Running shell command and capturing the output

I want to write a function that will execute a shell command and return its output as a string, no matter, is it an error or success message. I just want to get the same result that I would have gotten with the command line. What would be a code…
Silver Light
  • 44,202
  • 36
  • 123
  • 164
620
votes
31 answers

A non-blocking read on a subprocess.PIPE in Python

I'm using the subprocess module to start a subprocess and connect to its output stream (standard output). I want to be able to execute non-blocking reads on its standard output. Is there a way to make .readline non-blocking or to check if there is…
Mathieu Pagé
  • 10,764
  • 13
  • 48
  • 71
429
votes
11 answers

How to terminate a python subprocess launched with shell=True

I'm launching a subprocess with the following command: p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) However, when I try to kill using: p.terminate() or p.kill() The command keeps running in the background, so I was wondering how…
user175259
  • 4,641
  • 5
  • 20
  • 15
423
votes
15 answers

Store output of subprocess.Popen call in a string

I'm trying to make a system call in Python and store the output to a string that I can manipulate in the Python program. #!/usr/bin/python import subprocess p2 = subprocess.Popen("ntpq -p") I've tried a few things including some of the suggestions…
Mark
  • 7,306
  • 8
  • 45
  • 87
418
votes
9 answers

Python subprocess/Popen with a modified environment

I believe that running an external command with a slightly modified environment is a very common case. That's how I tend to do it: import subprocess, os my_env = os.environ my_env["PATH"] = "/usr/sbin:/sbin:" +…
Oren_H
  • 4,481
  • 2
  • 17
  • 8
407
votes
7 answers

Actual meaning of 'shell=True' in subprocess

I am calling different processes with the subprocess module. However, I have a question. In the following code: callProcess = subprocess.Popen(['ls', '-l'], shell=True) and callProcess = subprocess.Popen(['ls', '-l']) # without shell Both work.…
user225312
  • 126,773
  • 69
  • 172
  • 181
383
votes
32 answers

Using module 'subprocess' with timeout

Here's the Python code to run an arbitrary command returning its stdout data, or raise an exception on non-zero exit codes: proc = subprocess.Popen( cmd, stderr=subprocess.STDOUT, # Merge stdout and stderr stdout=subprocess.PIPE, …
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
373
votes
8 answers

How to use `subprocess` command with pipes

I want to use subprocess.check_output() with ps -A | grep 'process_name'. I tried various solutions but so far nothing worked. Can someone guide me how to do it?
zuberuber
  • 3,851
  • 2
  • 16
  • 20
365
votes
5 answers

How to hide output of subprocess

I'm using eSpeak on Ubuntu and have a Python 2.7 script that prints and speaks a message: import subprocess text = 'Hello World.' print text subprocess.call(['espeak', text]) eSpeak produces the desired sounds, but clutters the shell with some…
rypel
  • 4,686
  • 2
  • 25
  • 36
341
votes
12 answers

How do I pass a string into subprocess.Popen (using the stdin argument)?

If I do the following: import subprocess from cStringIO import StringIO subprocess.Popen(['grep','f'],stdout=subprocess.PIPE,stdin=StringIO('one\ntwo\nthree\nfour\nfive\nsix\n')).communicate()[0] I get: Traceback (most recent call last): File…
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
331
votes
7 answers

Retrieving the output of subprocess.call()

How can I get the output of a process run using subprocess.call()? Passing a StringIO.StringIO object to stdout gives this error: Traceback (most recent call last): File "", line 1, in File…
Jeffrey Aylesworth
  • 8,242
  • 9
  • 40
  • 57
315
votes
17 answers

Constantly print Subprocess output while process is running

To launch programs from my Python-scripts, I'm using the following method: def execute(command): process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output = process.communicate()[0] exitCode…
Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
305
votes
11 answers

read subprocess stdout line by line

My python script uses subprocess to call a linux utility that is very noisy. I want to store all of the output to a log file and show some of it to the user. I thought the following would work, but the output doesn't show up in my application…
deft_code
  • 57,255
  • 29
  • 141
  • 224
286
votes
23 answers

live output from subprocess command

I'm using a python script as a driver for a hydrodynamics code. When it comes time to run the simulation, I use subprocess.Popen to run the code, collect the output from stdout and stderr into a subprocess.PIPE --- then I can print (and save to a…
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
1
2 3
99 100