Shell commands help perform routine tasks, including system troubleshooting, system monitoring, backup & data loss recovery, etc. We can automate routine tasks on our system by executing the Linux shell commands inside the programs. Most people think we can only run shell commands in a bash or batch file. Fortunately, we execute shell commands in a Python environment.
In this tutorial, we will show you how to run shell commands in Python.
Table of Contents
- What is a Shell?
- How to execute shell commands in Python
- Run shell commands in Python using the os module
- Execute shell command using os.popen() in Python
- Execute shell commands in Python using the subprocess module
- The subprocess.Popen() method
What is a Shell?
Shell is a command line interface or tool that interacts and communicates with the operating system. Users can run text-based commands in this environment. The shell or command line interface offers various advanced features, such as piping and redirection, that are not available in graphical user interfaces.
In Linux, the shell environment is referred to as Terminal, and in Windows, it is known as Command Prompt. Terminal and command prompt are both shell environments in different operating systems.
How to execute shell commands in Python
Different methods are available to execute shell commands in Python few we have listed:
Run shell commands in Python using the os module
The os module helps you to access several low-level features of an operating system. Furthermore, change directories (
cd), check system information with the complete stat, list directories, create files, and more.
We can execute shell commands in Python directly on the Terminal or can create a new Python file and place all code in it. To execute the shell command using the
os.system(), follow the steps:
- Create a Python file shell_cmd.py.
- First, import the required os module in this Python file. The system function of the os module executes the shell command.
system()function takes only string as an argument. Inside this function, you can type the desired command to do a specific task.
- Run the Python (.py) file via the terminal. The corresponding output will display on the terminal window or command prompt.
The following syntax is used to execute the shell command using the os module:
import os os.system('type shell command to get output')
Let’s take an example. Here, we used the
os.system() method to execute the shell command
echo in Python.
Example # 01
# Importing os module import os # print message using os module os.system('echo "How to execute shell commands in Python"')
In the above code, the echo command shows a message on the terminal screen by using the os module in Python.
Example # 02
Here’s another example of how to use the os module to display the current timezone of your system.
# Importing os module import os # Display the current timezone using the 'timedatectl' Linux command os.system('timedatectl')
After running the above Python code, the current timezone of your system should display on your terminal.
Example # 03
In the following example, we explained how to get the list of files in the current directory using the
# Importing os module import os # list files using the 'ls' Linux shell command os.system('ls')
The system function executes the shell command
ls to get the list of files in the current directory.
Execute shell command using
os.popen() in Python
When we execute a shell command using the
os.popen() method in Python, it returns an open file object that we can see below:
import os os.popen('pwd') os.popen('pwd').read()
So, using the
read() method, we can easily read the content of the object.
Execute shell commands in Python using the subprocess module
subprocess.run() method is the recommended way to execute the shell commands in Python. The subprocess module is a component of the standard Python library. Using this module, you can launch new processes, connect and display standard output, standard errors, command piping, and get their return code. Moreover, it helps in executing the shell command and capturing their output and error.
Example # 01
Let’s take an example to see the working of the subprocess module in Python.
For example, we want to execute the echo shell command in Python using the subprocess module. So first, we need to import the subprocess module and then use the
subprocess.run() function to help execute the
echo shell command in Python.
Here, we executed the
echo shell command directly on the terminal without creating a Python file. We also used an argument
shell=True. When the parameter
shell=Trueis used, the command is executed in a sub-shell.
import subprocess subprocess.run('echo "how to use subprocess module"', shell=True )
If you will not pass an argument
shell=True it will show the following error:
subprocess.run('echo "hello world"')
But, if you don’t want to use any arguments, use the echo shell script in this way:
import subprocess subprocess.run(['echo’, 'hello world’] )
Example # 02
In this example, we are using the subprocess module to execute the
ls -lh shell in Python.
#importing subprocess module import subprocess # get list of files of all sizes subprocess.run("ls", "-lh")
This program displays a list of files of all sizes in a long format.
Example # 03 – Shell Command to Python Variable
Here, we take another example in which we execute the
cat shell command in Python using the subprocess module.
import subprocess # display file content using Linux cat command subprocess.run("cat", "shell_cmd.py")
You can also store the output in a variable. It stores the output of the
cat command in a variable
output_var and then prints the results. You can also find all the attributes of the
CompletedProcess object by using the
import subprocess # display file content using the Linux cat command and store results in a variable output_var=subprocess.run("cat", "shell_cmd.py") print(output_var.__dict__)
You can also execute the shell commands in Python using the
subprocess.Popen() method in the following way:
import subprocess subprocess.Popen(['ls -lh'], shell=True)
The above program displays the list of directories and files with sizes on the terminal.
In this tutorial, we explained the basics of how to execute shell commands in Python and retrieve the output. We discussed different methods that help us in executing the shell command using Python. These methods include
I hope this guide helped you understand shell commands and how to execute these shell commands in Python.