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
247
votes
8 answers

How to get exit code when using Python subprocess communicate method?

How do I retrieve the exit code when using Python's subprocess module and the communicate() method? Relevant code: import subprocess as sp data = sp.Popen(openRTSP + opts.split(), stdout=sp.PIPE).communicate()[0] Should I be doing this another way?
CarpeNoctem
  • 5,540
  • 8
  • 30
  • 33
243
votes
2 answers

How can I specify working directory for a subprocess

Is there a way to specify the running directory of command in Python's subprocess.Popen()? For example: Popen('c:\mytool\tool.exe', workingdir='d:\test\local') My Python script is located in C:\programs\python Is is possible to run…
icn
  • 17,126
  • 39
  • 105
  • 141
225
votes
2 answers

What's the difference between subprocess Popen and call (how can I use them)?

I want to call an external program from Python. I have used both Popen() and call() to do that. What's the difference between the two? My specific goal is to run the following command from Python. I am not sure how redirects work. ./my_script.sh >…
varunl
  • 19,499
  • 5
  • 29
  • 47
194
votes
5 answers

Difference between subprocess.Popen and os.system

What is the difference between subprocess.Popen() and os.system()?
Arovit
  • 3,579
  • 5
  • 20
  • 24
191
votes
2 answers

How to suppress or capture the output of subprocess.run()?

From the examples in docs on subprocess.run() it seems like there shouldn't be any output from subprocess.run(["ls", "-l"]) # doesn't capture output However, when I try it in a python shell the listing gets printed. I wonder if this is the default…
planetp
  • 14,248
  • 20
  • 86
  • 160
188
votes
3 answers

"OSError: [Errno 2] No such file or directory" while using python subprocess with command and arguments

I am trying to run a program to make some system calls inside Python code using subprocess.call() which throws the following error: Traceback (most recent call last): File "", line 1, in File…
Sandeep Mederametla
  • 1,891
  • 2
  • 12
  • 5
178
votes
24 answers

Getting realtime output using subprocess

I am trying to write a wrapper script for a command line program (svnadmin verify) that will display a nice progress indicator for the operation. This requires me to be able to see each line of output from the wrapped program as soon as it is…
Chris Lieb
  • 3,706
  • 7
  • 36
  • 48
172
votes
9 answers

Subprocess changing directory

I want to execute a script inside a subdirectory/superdirectory (I need to be inside this sub/super-directory first). I can't get subprocess to enter my subdirectory: tducin@localhost:~/Projekty/tests/ve$ python Python 2.7.4 (default, Sep 26 2013,…
ducin
  • 25,621
  • 41
  • 157
  • 256
169
votes
10 answers

How can I run an external command asynchronously from Python?

I need to run a shell command asynchronously from a Python script. By this I mean that I want my Python script to continue running while the external command goes off and does whatever it needs to do. I read this post: Calling an external command…
user76923
152
votes
4 answers

How to use subprocess popen Python

Since os.popen is being replaced by subprocess.popen, I was wondering how would I convert os.popen('swfdump /tmp/filename.swf/ -d') to subprocess.popen() I tried: subprocess.Popen("swfdump /tmp/filename.swf -d") subprocess.Popen("swfdump %s -d" %…
Stupid.Fat.Cat
  • 10,755
  • 23
  • 83
  • 144
143
votes
2 answers

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

subprocess.call(["/home/myuser/run.sh", "/tmp/ad_xml", "/tmp/video_xml"]) RIght now I have a script that I run. When I run it and it hits this line, it starts printing stuff because run.sh has prints in it. How do I pipe this to a text file also?…
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
134
votes
5 answers

What's the difference between Python's subprocess.call and subprocess.run

I've been trying to understand for a while now what's the difference between subprocess.call and subprocess.run. I know the last one is new on Python 3.5 and both are based on subprocess.Popen, but I'm not able to understand the difference yet.
Andrés Orozco
  • 2,490
  • 5
  • 32
  • 48
132
votes
6 answers

How to redirect output with subprocess in Python?

What I do in the command line: cat file1 file2 file3 > myfile What I want to do with python: import subprocess, shlex my_cmd = 'cat file1 file2 file3 > myfile' args = shlex.split(my_cmd) subprocess.call(args) # spits the output in the window i call…
catatemypythoncode
  • 1,323
  • 2
  • 9
  • 4
129
votes
4 answers

Why does Popen.communicate() return b'hi\n' instead of 'hi'?

Can someone explain why the result I want, "hi", is preceded with a letter 'b' and followed with a newline? I am using Python 3.3 >>> import subprocess >>> print(subprocess.Popen("echo hi", shell=True, …
imagineerThat
  • 5,293
  • 7
  • 42
  • 78
124
votes
10 answers

How to catch exception output from Python subprocess.check_output()?

I'm trying to do a Bitcoin payment from within Python. In bash I would normally do this: bitcoin sendtoaddress So for example: bitcoin sendtoaddress 1HoCUcbK9RbVnuaGQwiyaJGGAG6xrTPC9y 1.4214 If it is successful I get a…
kramer65
  • 50,427
  • 120
  • 308
  • 488