Summary: To call an external command in a Python script, use any of the following methods:
Whether you are a developer or a system administrator, automation scripts are going to be a common part of your daily routine.
These may include automating routine tasks like file backups or health checks. However, maintaining such shell scripts might become tedious and complex. This is where Python scripting comes in handy and opens up a whole new dimension to handle such tasks with ease. But for such scripts to work efficiently, we must learn to invoke external/shell commands in our Python scripts. Having said that, let us jump into our problem statement.
Problem: Given an external command that can run on your operating system; how to call the command using a Python script?
Example: Say, you want to ping a remote server using your operating system’s
ping command—all from within your Python program.
Python provides various ways to call and execute external shell commands. Without further delay, let’s discuss the various methods which can be used to invoke external commands in Python and the ideal scenarios to use them.
Method 1: Using The subprocess.call() Function
subprocess module is the recommended method of invoking and executing external commands in Python. It provides a flexible way of suppressing the input and output of various external/shell commands and, once invoked, it triggers new processes and then obtains their return codes.
You can use various functions with the subprocess module to invoke external commands using a Python script.
call() function of the subprocess module is used to start an external process, waits until the command completes, and then provides a return code. Thus the
subprocess.call() function is used to return an exit code which can then be used in the script to determine if the command executed successfully or it returned an error. Any return-code other than “0” means that there was an error in execution.
Let us have a look at the following program using the
call() function to check if the ping test is successful in the system :
import subprocess return_code = subprocess.call(['ping', 'localhost']) print("Output of call() : ", return_code)
Pinging DESKTOP-PC [::1] with 32 bytes of data: Reply from ::1: time<1ms Reply from ::1: time<1ms Reply from ::1: time<1ms Reply from ::1: time<1ms Ping statistics for::1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms Output of call() : 0
Method 2: Using The subprocess.run() Function
In Python 3.5 and above, the
run() function is the recommended method of invoking the
subprocess module to run an external command.
It is similar to the
call() function, however, unlike the
call() function it does not return an exception if the underlying process returns an error code. The
run() function is more flexible than the
call() function and instead of returning a return code, it returns a
CompletedProcess object once the code completes its execution.
import subprocess return_code = subprocess.run(['ping', 'localhost']) print("Output of run() : ", return_code)
Pinging DESKTOP-PC [::1] with 32 bytes of data: Reply from ::1: time<1ms Reply from ::1: time<1ms Reply from ::1: time<1ms Reply from ::1: time<1ms Ping statistics for::1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms Output of run() : CompletedProcess(args=['ping', 'localhost'], returncode=0)
Method 3: Using the subprocess.Popen Class
The use of
subprocess.Popen() is recommended only for advanced cases that cannot be handled by other methods like
subprocess.call(). This is because, on account of its comprehensive nature, it is more complicated and difficult to manage. Nevertheless, it can prove to be instrumental in dealing with complex operations.
Let us have a look at the following code that uses the
Popen() to open Microsoft Excel in Windows:
import subprocess subprocess.Popen("C:\Program Files (x86)\Microsoft Office\Office12\excel.exe")
Method 4: Using The os.system(command) Function
os module in Python provides several functions to interact with the shell and the system directly. Let us have a look at a couple of methods to invoke external commands using the
os.system() function helps to immediately interact with the shell by passing commands and arguments to the system shell. It returns an exit code upon completion of the command, which means that an exit code of 0 denotes successful execution while anything other than 0 means that the execution was unsuccessful. On one hand, this is a convenient approach since the
system() method can be used to run multiple commands at once using pipes and input/output redirection; while on the other hand, you must manually handle escape characters such as spaces.
Let us have a look at the following program which is used to display the current date of the system using the
import os dt = 'date' os.system(dt)
The current date is: 03-09-2020
You can try this yourself in our interactive online shell:
Exercise: Run the code. Why is the output different from the output on your own machine?
Method 5: Using The os.popen(“Command”) Function
os.popen() will behave the same as
os.system function, however, instead of returning the return code, it returns a file-like object which can be used to access the standard input/output for the process being executed. The
popen command allows us to interact with the system and create a pipe to or from another command. It has four different variants which are:
Let us have a look at the following program which uses
popen to print a string as output by invoking the shell command,
import os print (os.popen("echo Hello FINXTER!").read())
Try it yourself:
Exercise: Does the command echo has to be installed on the server for this to work?
Which Method Should You Use?
os.system(): If printing the output on the console is not a criterion and you need to run only a few simple commands you can use the
subprocess.run(): If you want to manage the input and output of an external command then use the
subprocess.Popen: If you want to manage a complex external command and also continue with your work, then you might want to use the
🌎 Recommended Tutorial: How to Import External Code in Python
Thus from the above discussion, we can conclude that the following methods can be used to call an external command in python :
The official documentation of Python recommends using the
subprocess module over the
os module functions since the
subprocess module provides several advantages over the
os module like more flexible options, better error handling, and much more.
I hope you found this article helpful and it helps you to understand how to invoke external commands in python. Stay tuned for more interesting concepts in the future.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.