python subprocess
Python hosting: Host, run, and code Python in the cloud!
The subprocess
module in Python allows users to spawn new applications, connect to their input/output/error pipes, and obtain their return codes. It’s a powerful tool that lets you interact with the system and other applications directly from your Python code.
If you’re aspiring to become proficient in Python programming, consider the Python Programming Bootcamp: Go from zero to hero.
Using Popen to Start a Process:
Popen
is a fundamental method within the subprocess
module. It helps you start external programs. The example below demonstrates how to start the Unix program ‘cat’ to read the contents of ‘test.py’. This is similar to executing ‘cat test.py’ in a terminal. In Python, you can initiate any program with any parameter using this approach.
1 | #!/usr/bin/env python |
Here, process.communicate()
captures both the output and any errors from the process. The stdout
variable contains the process’s output. If there’s an error, it’s captured in stderr
. If you want to ensure your script waits for the subprocess to finish before proceeding, you can use Popen.wait()
.
The call() Method in Subprocess:
subprocess
provides another method named call()
. This function is used to execute a program, wait for it to finish, and then return the return code. Below is its full definition:
1 | subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False) |
For instance, to execute the command “ls -l” using this method:
1 | #!/usr/bin/env python |
Capturing Process Output with check_output:
If you want to capture the output of an executed command directly into a string, you can use check_output
. Here’s its method definition:
1 | subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False) |
Here’s how you can use it:
1 | #!/usr/bin/env python |
By mastering the subprocess
module, you’ll be better equipped to create Python scripts that can seamlessly interact with other programs and the operating system.
Leave a Reply:
I just want to say These are the best tutorials of any anywhere. I use tutorials all the time and these are just so concise and effective. Thanks a lot and keep at it!
Thanks Kheenan!
Hi,Frank
Could tell me why?
I've copy it and run it . it show me:
Hi,
The first parameter of Popen() is 'cat', this is a unix program.
I'm not sure if this program is available on windows, try changing it to notepad.exe
Hi Frank,
i have found your website very useful as i am python learner. please if you could guide me the way to read pdf file in python. Secondly, i want tutorials about natural language processing in python. can you help in this regard.
Many Thanks
Hi Hina,
Python has no standard method to read pdf. To read pdf you need to use a module. You can find related projects here: https://github.com/search?l...
Alternatively you can convert the pdf document to a txt method, using the Pdftotext tool: https://en.wikipedia.org/wi...
If you start notepad.exe as a windowed app then python will not get the output.
The MSDOS command similar to "cat" is "type".
Thanks Francisco!
Hi frank. I wanted to know if i could store many values using (check_output). I want to use ping operation in cmd as subprocess and store the ping statistics in the variable to use them.
Hi Rehman, you can store the entire output like this:
Python 3 returns this as bytes, so we convert it to string with the function decode.
Then we split on the newline character:
Now we have a few strings. We can parse the line or lines we want. We'll split that string into parts.
I'll take the statistics summary.
All variables are stored in the list values. You can then store them in variables if you want. v[0] contains the first variable of the line.
Hey Frank !
Thank you for the tutorial. It has been quite helpfull in understanding the subprocess module, but now
i found a new problem and am wondering if you might have an idea for a solution ? I want to open an exe and pass it a .tcl file filled with commands for the .exe to use. By searching the whole internet I still havent ound a way that works. Do you might have an idea ?
Kind regards,
Laura
You might have luck with command line arguments and include them in the subprocess call
There are two coding errors on this page:
It should be 'print(stdout)' not 'print stdout'
'print('s = ' + s)' throws an error message 'TypeError: can only concatenate str (not 'bytes') to str'
Thanks, Python is not backwards compatible and thats why you got these errors. I have updated the page