Learn Python – How To Call an External Command?

Many times is necessary that Python interacts with the computer’s operating system where it is running. For example, to have information about the hard drive partitions. For this to happen, you must create an external command, which invokes the information or program that you want, directly from the OS.

In this post, we will show a Python module that allows processes to be created. These processes call external programs outside the code written in Python. It also allows the output of this program to be seen on the screen, and be run as if in a normal shell. This is the subprocess module, which offers several options to run external programs. We will show here two of them:

Python – The call option ()

This option shows on the screen the information that the external program has generated, but not as an entry that can be manipulated later by our program in Python. The value that the call () function returns within the Python program is only the status of implementation of the external program.

In order to manipulate the data from this external program, we use the check_output () function. An example using this function in place of call () is shown below:

This example is calling a Java program, whose information will be handled later in Python, being provided by return.


Python – The Popen option ()

It offers great flexibility for developers to be able to handle more complex cases which are not covered by the simplest functions. The subprocess.Popen () runs a child program in a new process. At Unix, it has a similar behavior to os.execvp () – when running the external program. At Windows, it uses the CreateProcess () function.

These are some of the ways to call an external command in Python. Do you know a different way? Share with us in the comments area below!

If you want to look for more tips, search our videos about Python. Below are some examples:

Aerolith.org – (Part 1)


Working on backend API

You can also follow some of our broadcasters who program in Python, as below:

writkas writkas







Another cool way to find out interesting things about Python is to access our project page!


About author

About Author Dr. Michael Jurgen Garbade is the founder of LiveEdu.TV, Kyuda, Education Ecosystem. He is future Venture Capitalist, Future Politician and always on the lookout for the Next Big Challenge. Obtained Masters in business administration and physics, and a Ph.D. in finance with professional work experience in high-paced environments at Fortune 500 companies like Amazon and General Electric. Expertize: Python, PHP, Sencha Touch & C++, SEO, Finance, Strategy & E-commerce. He speaks English and German and has worked in the US, Europe, and Asia. At Education Ecosystem he is the CEO and runs business operations.