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
71
votes
5 answers

OSError: [Errno 8] Exec format error

I am having hard time parsing the arguments to subprocess.Popen. I am trying to execute a script on my Unix server. The script syntax when running on shell prompt is as follows: /usr/local/bin/script hostname = -p LONGLIST. No matter how…
user3477108
  • 877
  • 1
  • 6
  • 6
71
votes
5 answers

Non blocking subprocess.call

I'm trying to make a non blocking subprocess call to run a slave.py script from my main.py program. I need to pass args from main.py to slave.py once when it(slave.py) is first started via subprocess.call after this slave.py runs for a period of…
DavidJB
  • 2,272
  • 12
  • 30
  • 37
70
votes
5 answers

Popen error: "[Errno 2] No such file or directory" when calling shell function

I have some custom commands. This works: subprocess.Popen(['python'], stdout=subprocess.PIPE) But if I have my own system commands like deactivate, I get that error Traceback (most recent call last): File "runner2.py", line 21, in
user1012451
  • 3,343
  • 7
  • 29
  • 33
69
votes
4 answers

ImageMagick not authorized to convert PDF to an image

I have a program, in which I need to convert a PDF to an image using Image Magick. I do that using the subprocess package: cmd = 'magick convert -density 300 '+pdfFile+'['+str(rangeTuple[0])+'-'+str(rangeTuple[1])+'] -depth 8 '+'temp.tiff'…
Mooncrater
  • 4,146
  • 4
  • 33
  • 62
69
votes
5 answers

Python3 subprocess output

I want to run the Linux word count utility wc to determine the number of lines currently in the /var/log/syslog, so that I can detect that it's growing. I've tried various test, and while I get the results back from wc, it includes both the line…
user2565677
  • 875
  • 1
  • 8
  • 15
68
votes
4 answers

When to use each method of launching a subprocess in Ruby

1. `` The Backtick defined in Kernel 1. a) %x{} Percent X < alternate syntax for The Backtick defined in parse.y, see discussion 2. system() Kernel#system 3. fork() Kernel#fork, Process#fork 4. open() open a pipe Kernel#open 4.a.…
Alec Wenzowski
  • 3,878
  • 3
  • 25
  • 40
67
votes
9 answers

Python subprocess: callback when cmd exits

I'm currently launching a programme using subprocess.Popen(cmd, shell=TRUE) I'm fairly new to Python, but it 'feels' like there ought to be some api that lets me do something similar to: subprocess.Popen(cmd, shell=TRUE, …
Who
  • 695
  • 1
  • 6
  • 6
66
votes
7 answers

Passing double quote shell commands in python to subprocess.Popen()?

I've been trying to pass a command that works only with literal double quotes in the commandline around the "concat:file1|file2" argument for ffmpeg. I cant however make this work from python with subprocess.Popen(). Anyone have an idea how one…
Fight Fire With Fire
  • 1,626
  • 3
  • 19
  • 28
63
votes
7 answers

Python read from subprocess stdout and stderr separately while preserving order

I have a python subprocess that I'm trying to read output and error streams from. Currently I have it working, but I'm only able to read from stderr after I've finished reading from stdout. Here's what it looks like: process =…
Leah Sapan
  • 3,621
  • 7
  • 33
  • 57
63
votes
5 answers

output the command line called by subprocess?

I'm using the subprocess.Popen call, and in another question I found out that I had been misunderstanding how Python was generating arguments for the command line. My Question Is there a way to find out what the actual command line was? Example…
Brian Postow
  • 11,709
  • 17
  • 81
  • 125
61
votes
4 answers

Run Process and Don't Wait

I'd like to run a process and not wait for it to return. I've tried spawn with P_NOWAIT and subprocess like this: app = "C:\Windows\Notepad.exe" file = "C:\Path\To\File.txt" pid = subprocess.Popen( [app, file], shell=True, …
Bullines
  • 5,626
  • 6
  • 53
  • 93
60
votes
14 answers

Using sudo with Python script

I'm trying to write a small script to mount a VirtualBox shared folder each time I execute the script. I want to do it with Python, because I'm trying to learn it for scripting. The problem is that I need privileges to launch mount command. I could…
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207
59
votes
10 answers

how to kill (or avoid) zombie processes with subprocess module

When I kick off a python script from within another python script using the subprocess module, a zombie process is created when the subprocess "completes". I am unable to kill this subprocess unless I kill my parent python process. Is there a way…
Dave
  • 1,480
  • 3
  • 16
  • 26
59
votes
3 answers

How to write to stdout AND to log file simultaneously with Popen?

I am using Popen to call a shell script that is continuously writing its stdout and stderr to a log file. Is there any way to simultaneously output the log file continuously (to the screen), or alternatively, make the shell script write to both the…
imagineerThat
  • 5,293
  • 7
  • 42
  • 78
58
votes
10 answers

Using a Python subprocess call to invoke a Python script

I have a Python script that needs to invoke another Python script in the same directory. I did this: from subprocess import call call('somescript.py') I get the following error: call('somescript.py') File "/usr/lib/python2.6/subprocess.py", line…
user514946
  • 1,165
  • 3
  • 12
  • 12