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
121
votes
2 answers

wait process until all subprocess finish?

I have a main process which creates two or more sub processes, I want main process to wait until all sub processes finish their operations and exits? # main_script.py p1 = subprocess.Popen(['python script1.py']) p2 = subprocess.Popen(['python…
Nikhil Rupanawar
  • 4,061
  • 10
  • 35
  • 51
115
votes
3 answers

Pipe subprocess standard output to a variable

I want to run a command in pythong, using the subprocess module, and store the output in a variable. However, I do not want the command's output to be printed to the terminal. For this code: def storels(): a =…
Insomaniacal
  • 1,907
  • 3
  • 14
  • 10
115
votes
5 answers

Is there a way to check if a subprocess is still running?

I'm launching a number of subprocesses with subprocess.Popen in Python. I'd like to check whether one such process has completed. I've found two ways of checking the status of a subprocess, but both seem to force the process to complete. One is…
bjarkemoensted
  • 2,557
  • 3
  • 24
  • 37
115
votes
3 answers

What is the difference between multiprocessing and subprocess?

My work should use parallel techniques, and I am new user of Python. I wonder if you could share some material about the Python multiprocessing and subprocess modules. What is the difference between these two?
Jun HU
  • 3,176
  • 6
  • 18
  • 21
111
votes
13 answers

catching stdout in realtime from subprocess

I want to subprocess.Popen() rsync.exe in Windows, and print the stdout in Python. My code works, but it doesn't catch the progress until a file transfer is done! I want to print the progress for each file in real time. Using Python 3.1 now since I…
John A
  • 1,111
  • 2
  • 8
  • 4
108
votes
6 answers

How do I write to a Python subprocess' stdin?

I'm trying to write a Python script that starts a subprocess, and writes to the subprocess stdin. I'd also like to be able to determine an action to be taken if the subprocess crashes. The process I'm trying to start is a program called nuke which…
jonathan topf
  • 7,897
  • 17
  • 55
  • 85
108
votes
7 answers

Python popen command. Wait until the command is finished

I have a script where I launch with popen a shell command. The problem is that the script doesn't wait until that popen command is finished and go continues right away. om_points = os.popen(command, "w") ..... How can I tell to my Python script to…
michele
  • 26,348
  • 30
  • 111
  • 168
102
votes
4 answers

Is it possible to run function in a subprocess without threading or writing a separate file/script.

import subprocess def my_function(x): return x + 100 output = subprocess.Popen(my_function, 1) #I would like to pass the function object and its arguments print output #desired output: 101 I have only found documentation on opening…
wroscoe
  • 1,914
  • 3
  • 19
  • 18
97
votes
7 answers

Read streaming input from subprocess.communicate()

I'm using Python's subprocess.communicate() to read stdout from a process that runs for about a minute. How can I print out each line of that process's stdout in a streaming fashion, so that I can see the output as it's generated, but still block…
Heinrich Schmetterling
  • 6,614
  • 11
  • 40
  • 56
95
votes
3 answers

subprocess: unexpected keyword argument capture_output

When executing subprocess.run() as given in the Python docs, I get a TypeError: >>> import subprocess >>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True) Traceback (most recent call last): File "", line 1, in File…
Tijs
  • 1,257
  • 1
  • 8
  • 13
91
votes
1 answer

Blocking and Non Blocking subprocess calls

I'm completely confused between subprocess.call() , subprocess.Popen(), subprocess.check_call(). Which is blocking and which is not ? What I mean to say is if I use subprocess.Popen() whether the parent process waits for the child process to…
Kumar Roshan Mehta
  • 3,078
  • 2
  • 27
  • 50
87
votes
8 answers

How to replicate tee behavior in Python when using subprocess?

I'm looking for a Python solution that will allow me to save the output of a command in a file without hiding it from the console. FYI: I'm asking about tee (as the Unix command line utility) and not the function with the same name from Python…
sorin
  • 161,544
  • 178
  • 535
  • 806
86
votes
3 answers

Understanding Popen.communicate

I have a script named 1st.py which creates a REPL (read-eval-print-loop): print "Something to print" while True: r = raw_input() if r == 'n': print "exiting" break else: print "continuing" I then launched 1st.py…
Black_Hat
  • 943
  • 1
  • 9
  • 12
85
votes
3 answers

What can lead to "IOError: [Errno 9] Bad file descriptor" during os.system()?

I am using a scientific software including a Python script that is calling os.system() which is used to run another scientific program. While the subprocess is running, Python at some point prints the following: close failed in file object…
Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130
84
votes
3 answers

subprocess wildcard usage

import os import subprocess proc = subprocess.Popen(['ls','*.bc'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out,err = proc.communicate() print out This script should print all the files with .bc suffix however it returns an empty list.…
Cemre Mengü
  • 18,062
  • 27
  • 111
  • 169