Executing Shell Commands with Python
This article starts with a basic introduction to Python shell commands and why one should use them. It also describes the three primary ways to run Python shell commands.
- os.system()
- subprocess.run()
- subprocess.Popen()
What is a shell in the os?
In programming, the shell is a software interface for accessing the functionality of the operating system. Shells in the operating system can be either a CLI (Command Line Interface) or a GUI (Graphical User Interface) based on the functionality and basic operation of the device.
Executing Shell Commands with Python using the subprocess module
The Python subprocess module can be used to run new programs or applications. Getting the input/output/error pipes and exit codes of different commands is also helpful.
subprocess.Popen()
Here. we are using the subprocess.Popen() method to execute the echo shell script using Python. You can give more arguments to the Popen function Object() , like shell=True, which will make the command run in a separate shell.
# Importing required module
import subprocess
# Using system() method to
# execute shell commands
subprocess.Popen('echo "Geeks 4 Geeks"', shell=True)
Output:

subprocess.run()
Here. we are using the system() method to execute the pwd shell script using Python. run() is more flexible and quicker approach to run shell scripts, utilise the Popen function.
# Importing required module
import subprocess
# Using system() method to
# execute shell commands
subprocess.run(["powershell", "pwd"], shell=True)
Output:
Executing Shell Commands with Python using the os module
The os module in Python includes functionality to communicate with the operating system. It is one of the standard utility modules of Python. It also offers a convenient way to use operating system-dependent features, shell commands can be executed using the system() method in the os module.
Example 1:
Here. we are using the system() method to execute shell commands of echo.
# Importing required module
import os
os.system('echo "Geeks 4 Geeks"')
Output:

Example 2:
Here, we are using the system() method to execute the PWD shell script using Python.
# Importing required module
import os
os.system('pwd')
Output:

Example 3:
Here. we are using the system() method to execute the cat shell script using Python.
# Importing required module
import os
os.system('cat')
Output:

Executing Shell Commands with Python – FAQs
Can You Execute Shell Commands in Python?
Yes, you can execute shell commands in Python using various modules provided by Python’s standard library, such as
subprocess
,os.system
, and others. Thesubprocess
module is recommended for its flexibility and ability to interact with the command’s input/output/error pipes.
How to Run Shell Command in Python Using Subprocess?
The
subprocess
module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. Here’s how to use it to run shell commands:Example:
import subprocess
# Running a shell command and capturing its output
result = subprocess.run(['ls', '-l'], capture_output=True, text=True)
print(result.stdout)This uses
subprocess.run()
, which is the recommended approach for most needs as it is high-level and versatile.
How to Run Linux Commands in Python Script?
Running Linux commands from a Python script can be done using the
subprocess
module as shown above. Here’s an example of running a simple Linux command:Example:
import subprocess
# Running the 'ls' command
subprocess.run(['ls', '-l'])This will execute the
ls -l
command and display its output in the console.
How Do I Run a Shell Command on a Remote Machine in Python?
To run a shell command on a remote machine, you can use the
paramiko
library, which is a Python implementation of the SSHv2 protocol, providing both client and server functionality.Example Using Paramiko:
import paramiko
# Create a new SSH client
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the remote host
client.connect('hostname_or_IP', username='username', password='password')
# Run a command (`uname -a` as an example)
stdin, stdout, stderr = client.exec_command('uname -a')
print(stdout.read().decode())
# Close the connection
client.close()This script connects to a remote server using SSH and executes the
uname -a
command.
How Do I Run a Shell Command in Jupyter Python?
In Jupyter notebooks, you can run shell commands directly in a cell by prefixing the command with
!
. This is a quick and effective way to interact with the system shell.Example in a Jupyter Cell:
!ls -l
This will execute the
ls -l
command and display its output directly in the Jupyter notebook.