Executing Shell Commands with Python

Pankaj Singh Last Updated : 16 Jan, 2024
3 min read

Introduction

Python stands out as a high-level, interpreted, and general-purpose language that empowers developers to achieve various tasks efficiently, including executing shell commands. Whether you want to automate system administration tasks, interact with the operating system, or run external programs, Python provides a simple and efficient way to achieve these goals. This blog will explore the various methods and best practices for executing shell commands with Python.

Shell Command

What is Shell?

A shell is a crucial interface connecting users with the underlying operating system kernel, enabling interaction with a computer’s resources and services. Functioning as an interpreter, it translates text-based commands input by users into executable actions for the operating system. The shell provides a gateway to control and manage various aspects such as files, processes, system settings, etc. 

Different operating systems have distinct shells, with Windows utilizing Command Prompt (cmd.exe) and PowerShell, macOS employing Terminal with bash or zsh, and Linux offering various options including bash, zsh, tcsh, and fish. 

What are Shell Commands?

Shell commands, consisting of specific words or phrases, instruct the shell to carry out tasks across a spectrum of functions. These commands encompass file management (ls, cd, mkdir, rm, cp, mv, cat, less, more, touch), process management (ps, top, kill, jobs, fg, bg), system information retrieval (uname, whoami, date, cal, uptime, free, df), network tools (ping, netstat, ifconfig, traceroute, dig, wget, curl), text processing (grep, sed, awk, head, tail, sort, uniq), user management (useradd, usermod, userdel, passwd), and software management with package managers like apt, yum, dnf, pacman, and brew. Additionally, the shell allows users to craft custom commands, including scripts and functions, for automation purposes.

Understanding the Basics

When it comes to executing shell commands with Python, there are several different approaches you can take. One of the most common methods is to use the `subprocess` module, which allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module provides a high-level interface for running shell commands and accessing the input/output streams of the subprocess.

Another approach is to use the `os.system` function, which allows you to execute shell commands directly from Python. While this method is simple, it has some limitations compared to the `subprocess` module. We will explore the pros and cons of each approach and discuss the scenarios in which they are most suitable.

Execution Using the Subprocess Module

The `subprocess` module is a powerful and flexible way to execute shell commands with Python. It provides a rich set of functions and classes for working with subprocesses, including the ability to run commands, capture their output, and handle errors.

We will walk through the process of using the `subprocess` module to execute the commands, covering topics such as running simple commands, capturing output, and handling errors.

Example

import subprocess
# Run a simple shell command
subprocess.run(["ls", "-l"])
# Capture the output of a command
result = subprocess.run(["echo", "Hello, World!"], capture_output=True, text=True)
print(result.stdout)
# Handling errors
try:
subprocess.run(["rm", "/nonexistentfile"])
except subprocess.CalledProcessError as e:
print(f"An error occurred: {e}")

Output

Hello, World!

Direct Execution with os.system

While the `subprocess` module provides a more sophisticated way to execute shell commands, the `os.system` function offers a simple and direct approach. This function allows you to run shell commands directly from Python, without the need to create subprocesses or handle input/output streams explicitly. We will explore the basics of using `os.system` to execute shell commands and discuss its limitations compared to the `subprocess` module.

Example

import os

# Run a shell command

os.system("ls")

Conclusion

Executing shell commands with Python opens up possibilities for automating tasks, interacting with the operating system, and running external programs. By leveraging the `subprocess` module and other Python features, you can execute shell commands efficiently and securely. Whether you are a system administrator, developer, or data scientist, mastering the art of executing shell commands with Python is a valuable skill that can enhance your productivity and expand your capabilities.

Supercharge your career with Python – the leading language in data science. Unlock the door to data science by enrolling in our beginner-friendly Python course. No coding or data science background is required. Enroll for free and kickstart your data science journey today!

Hi, I am Pankaj Singh Negi - Senior Content Editor | Passionate about storytelling and crafting compelling narratives that transform ideas into impactful content. I love reading about technology revolutionizing our lifestyle.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details