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
83
votes
7 answers

running multiple bash commands with subprocess

If I run echo a; echo b in bash the result will be that both commands are run. However if I use subprocess then the first command is run, printing out the whole of the rest of the line. The code below echos a; echo b instead of a b, how do I get it…
Paul
  • 5,756
  • 6
  • 48
  • 78
82
votes
15 answers

Ensuring subprocesses are dead on exiting Python program

Is there a way to ensure all created subprocess are dead at exit time of a Python program? By subprocess I mean those created with subprocess.Popen(). If not, should I iterate over all of the issuing kills and then kills -9? anything cleaner?
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
82
votes
2 answers

Subprocess check_output returned non-zero exit status 1

This is my python code: import subprocess subprocess.check_output("ls",shell=True,stderr=subprocess.STDOUT) import subprocess subprocess.check_output("yum",shell=True,stderr=subprocess.STDOUT) The first .check_output() works well, but the second…
Zongze Wu
  • 953
  • 1
  • 6
  • 8
81
votes
4 answers

python subprocess Popen environment PATH?

Suppose there's an executable and a Python script to launch it, and they're located in 'sibling' subdirectories, e.g. /tmp/subdir1/myexecutable /tmp/subdir2/myscript.py If in /tmp and running python subdir2/myscript.py with a relative path to…
wim
  • 338,267
  • 99
  • 616
  • 750
81
votes
7 answers

Launch a shell command with in a python script, wait for the termination and return to the script

I have a python script that has to launch a shell command for every file in a dir: import os files = os.listdir(".") for f in files: os.execlp("myscript", "myscript", f) This works fine for the first file, but after the "myscript" command has…
Davide Gualano
  • 12,813
  • 9
  • 44
  • 65
81
votes
5 answers

Run subprocess and print output to logging

I am looking for the way to call shell scripts from python and write their stdout and stderr to file using logging. Here is my code: import logging import tempfile import shlex import os def run_shell_command(command_line): command_line_args =…
Kostya
  • 1,536
  • 1
  • 13
  • 22
80
votes
14 answers

OSError: [WinError 193] %1 is not a valid Win32 application

I am trying to call a Python file "hello.py" from within the python interpreter with subprocess. But I am unable to resolve this error. [Python 3.4.1]. import subprocess subprocess.call(['hello.py', 'htmlfilename.htm']) Traceback (most recent…
Caxton
  • 1,050
  • 1
  • 8
  • 10
80
votes
6 answers

File not found error when launching a subprocess containing piped commands

I need to run the command date | grep -o -w '"+tz+"'' | wc -w using Python on my localhost. I am using subprocess module for the same and using the check_output method as I need to capture the output for the same. However it is throwing me an error…
h4ck3d
  • 6,134
  • 15
  • 51
  • 74
79
votes
5 answers

How do I hide the console when I use os.system() or subprocess.call()?

I wrote some statements like below: os.system(cmd) #do something subprocess.call('taskkill /F /IM exename.exe') both will pop up a console. How can I stop it from popping up the console?
Synapse
  • 1,574
  • 6
  • 18
  • 19
78
votes
3 answers

python getoutput() equivalent in subprocess

I want to get the output from some shell commands like ls or df in a python script. I see that commands.getoutput('ls') is deprecated but subprocess.call('ls') will only get me the return code. I'll hope there is some simple solution.
Rafael T
  • 15,401
  • 15
  • 83
  • 144
77
votes
3 answers

How do you return to a sourced bash script?

I use "source" inside a bash script, as follows: #!/bin/bash source someneatscriptthatendsprematurely.sh I would like to exit from the someneatscriptthatendsprematurely.sh script, without exiting from the main script. Any help appreciated!
codar
  • 7,018
  • 2
  • 18
  • 8
76
votes
10 answers

Python Subprocess: Too Many Open Files

I am using subprocess to call another program and save its return values to a variable. This process is repeated in a loop, and after a few thousands times the program crashed with the following error: Traceback (most recent call last): File…
Vahid Mirjalili
  • 6,211
  • 15
  • 57
  • 80
75
votes
9 answers

How do I use subprocess.Popen to connect multiple processes by pipes?

How do I execute the following shell command using the Python subprocess module? echo "input data" | awk -f script.awk | sort > outfile.txt The input data will come from a string, so I don't actually need echo. I've got this far, can anyone…
Tom
  • 42,844
  • 35
  • 95
  • 101
75
votes
6 answers

subprocess.check_output return code

I am using: grepOut = subprocess.check_output("grep " + search + " tmp", shell=True) To run a terminal command, I know that I can use a try/except to catch the error but how can I get the value of the error code? I found this on the official…
Juicy
  • 11,840
  • 35
  • 123
  • 212
74
votes
4 answers

Is there a quiet version of subprocess.call?

Is there a variant of subprocess.call that can run the command without printing to standard out, or a way to block out it's standard out messages?
fergusdawson
  • 1,645
  • 3
  • 17
  • 20