Register Login

Executing Shell Commands with Python

Updated Aug 06, 2021

Python is a robust programming language that allows to script and automate tasks using its different libraries. System admins find this programming language very convenient because it allows developers and system administrators to execute system-related operations easily. In this article, you will learn about executing shell commands in Python.

What are shells?

Shells are programs that has an interface to access the various functions of an operating system. Operating systems like Linux and Windows have shells. These shells can be either command-line interface (CLI) or graphical user interface (GUI) based on the functionality and basic operations required to execute.

Which Python module allows executing shell commands?

Python allows developers to easily communicate with the operating system by executing shell commands from a Python program. We can achieve this using two different modules:

  • os module
  • subprocess module

Repeating any OS-based tasks or system-related tasks multiple times becomes tedious if the admin has to do it again and again. In such a situation, they have to develop a Python program that can allow them to access such system operations by executing shell programs using Python code. Let us first start using the os module and see how to implement different operations through it.

Method 1: Using the os module in Python:

The os module of Python is an essential module that helps programmers to interact with the operating system. This module comes as a part of Python’s standard modules. It essentially provides a manageable way to use all the dependant functions of the operating system through a Pythons script. There are various methods and functions available within the os and os.path modules that help programmers to interact with the system operations and calls.

Let us now use the shell’s echo command to print some messages from within the Python code:

import os
os.system("echo Printing the programmer's name STechies!")

Output:

Printing the programmer's name STechies!

Now, if you wish to list all the files in your Unix and Linux-based operating systems, the Python code to trigger the shell command is

import os
os.system('ls')

If you wish to check the path in which you are working, the getcwd() method will help you fetch the current working directory or present working directory.

import os
pwd = os.getcwd()
print("Your present working directory is:", pwd)

Output:

Your present working directory is: D:\python

Method 2: Using the subprocess module in Python:

Python also allows programmers to spawn new processes, link to their input, output, and error pipes, and then receiving their return codes using the subprocess module. The subprocess modules work smoothly with both 2.x and 3.x. It is a recommended module for executing shell commands through Python. It gives us the adaptability to contain the shell commands' output, as well as linked the inputs and outputs of multiple commands concurrently. Let us take a look at the program of how to use it.

Program:

import shlex, subprocess
command_line = input("Enter your command : ")
argu = shlex.split(command_line)
print(argu)
ex = subprocess.popen(argu)

Output:

Explanation:

First, we have to import the shlex (shell executable) and the subprocess modules in our Python program. Then, we have created a variable called command_line which will store values taken through an input. Also, it will display a message while taking input from user “Enter your command :”. Next, shlex,split() will split the string passed as its parameter into a shell-like syntax and list it out. Then, the print() function will display your command. The subprocess.Popen() method will execute the shell command passed within it as parameter.

Conclusion:

Both os and subprocess modules are useful to perform shell execution, but the subprocess module is advantageous over os module because subprocess module has the flexibility to get the stdout, stderr, and various status codes, along with better error handling values incorporated within it.


×