If you’re a computer nerd, you might have played a lot with the command line (or the command prompt). I have done it a lot since I was a computer science student, and we had subjects like operating systems and shell programming back in college.
What we usually do is we open up the command line and type in the various command to do the required operations. Some brilliant people do everything using shell commands, without even looking into the UI of the operating system.
But can we execute shell commands using Python language? We’re going to talk about it in today’s article. As you might know, Python has a lot of in-built libraries to help us. There are multiple ways to execute a shell command and get output using Python.
Hence, it is a fabulous language for scripting and automating workflows. Python Standard Library has modules such as os and subprocess to make executing shell commands an easy task.
Let’s dive deep into the topic and learn about os and subprocess modules in python.
Executing Shell Commands Using the OS Module in Python
Python provides an OS library that we can use for the execution of shell commands. This method is pretty easy to implement using a few lines of code. Let’s execute a basic command using this module.
The python code for executing commands is pretty simple. You just need to import the OS module and use a method called system. You can pass the command that you want to execute as an argument to this method.
import os os.system('dir')
I have a Windows operating system. That’s why I used the ‘dir’ command. If you have Linux or Mac systems, you can use the ‘ls’ command (or any other basic commands).
After writing these two lines, save the program. Go to your command line (or command prompt) to do the execution.
If you are inside the command line, go to the directory, which is having the program, using the ‘cd’ command. I saved my file inside Desktop. So, I used the following command:
Now, let’s run the program by typing in the name of the program and hit Enter.
The program will run and execute the shell command. You can see the result inside your command line.
As you can see in the screenshot given above, the shell command executed perfectly inside my command prompt using a python program.
However, the OS module is not recommended for executing shell commands due to several reasons. This method is inflexible as you can’t even get the resulting output as a variable.
Python has another module called subprocess, which is the most recommended module for running external commands. So, let’s jump into the subprocess module and learn a bit about it.
Executing Shell Commands Using the Subprocess Module in Python
According to the official documentation of Python, the subprocess module is the best and recommended python module you can use for running shell commands. This module is pretty useful for system administration tasks, and it is a replacement for several OS methods.
The subprocess module helps us by providing great facilities to spawn processes, connect to their input/output/error pipes, and obtain their return codes. It starts new applications or programs through Python code by creating new processes.
When we use the subprocess module, we can also capture the output of the commands or even pipe the output from one command to another command. It is a great advantage when we compare it with the OS module.
Now, let’s see an example program to run a command in command prompt.
The job is pretty simple. Import the subprocess module and use one of its methods called run(). Pass the command that you want to execute as the argument to this method.
import subprocess subprocess.run('ls')
If you’re on a Mac or Linux, this command will execute perfectly. But, if you’re on a Windows machine and you tried executing the dir command (dir is the equivalent of ls command in Windows), then you’ll get an error like this:
FileNotFoundError: [WinError 2] The system cannot find the file specified
This error occurs because the dir command is built into the shell of the operating system. Hence, you need to pass another argument to this method, which is shell = True.
So, the correct program in Windows would look like this:
import subprocess subprocess.run('dir', shell=True)
When you run this program in your command prompt, you’ll get an output like this:
It is perfectly fine to use this argument in Mac/Linux too. But, it’s optional in those operating systems.
We can also pass any arguments(arguments for the shell command) to the command as well. An example would look like this (Linux/Mac):
import subprocess subprocess.run('ls -la', shell = True)
We can do it in this way only if we’re using the shell=True argument. If we aren’t using that, we need to pass in these commands and arguments as a list.
import subprocess subprocess.run(['ls', '-la'])
As I have said before, we can capture the output of a command in a variable. So, let’s create a variable and check whether we can do it or not (in Windows).
import subprocess x = subprocess.run('dir', shell=True) print(x)
If you want to use it on Linux/Mac, you can use the same code and replace ‘dir’ with ‘ls’.
When we do it in this way, we’ll get an output in the command line like this:
As you can see, we will get an additional line at the end, which says CompletedProcess(args=’dir’, returncode=0).
We can now use this variable to get all sorts of information about the command that we’ve executed. For that, we can use arguments like args (to check the arguments we’ve passed), returncode (to check errors), stdout (to check the standard output), etc.
Let’s see an example.
import subprocess x = subprocess.run('dir', shell=True) print(x.args)
When you run the code, you’ll see an output like this:
As you can see at the bottom, the information about arguments is printed.
If you want to invoke subprocesses, the recommended approach is to use the run() method for all use cases it can handle. However, in some advanced use cases, the Popen interface can be used directly.
Popen offers a lot of flexibility so that developers can handle the less common cases not covered by the convenience functions. You can learn more about it by reading the Python documentation.
Running Linux commands from the Python script is a big help for the system admins as most of their tasks involve Linux commands all the time.
This article was just an introductory guide to executing shell commands with Python. I don’t want to overwhelm you with all sorts of big nerdy programs.
If you didn’t know about this functionality of Python before, I hope you got some value out of this article. If you want me to make some more advanced articles related to this topic, let me know in the comments.
For all those nerds, you can check out the official documentation of the subprocess module of Python anytime and learn more about it. You can also learn about the basic shell commands in Linux and Windows.
If you have any doubts or suggestions, we can discuss those in the comments section.
I would appreciate it if you would be willing to share this article. It will encourage me to create more useful tutorials like this.
Thanks for reading. Happy coding!