python subprocess
Python hosting: Host, run, and code Python in the cloud!
The subprocess module enables you to start new applications from your Python program. How cool is that?
Related Course:
Python Programming Bootcamp: Go from zero to hero
Start a process in Python:
You can start a process in Python using the Popen function call. The program below starts the unix program ‘cat’ and the second parameter is the argument. This is equivalent to ‘cat test.py’. You can start any program with any parameter.
#!/usr/bin/env python |
The process.communicate() call reads input and output from the process. stdout is the process output. stderr will be written only if an error occurs. If you want to wait for the program to finish you can call Popen.wait().
Subprocess call():
Subprocess has a method call() which can be used to start a program. The parameter is a list of which the first argument must be the program name. The full definition is:
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False) |
In the example below the full command would be “ls -l”
#!/usr/bin/env python |
Save process output (stdout)
We can get the output of a program and store it in a string directly using check_output. The method is defined as:
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False) |
Example usage:
#!/usr/bin/env python |
If you are new to Python programming, I highly recommend this book.
Posted in Beginner
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.