There are numerous advantages to utilizing Python for system administration. Python’s capability to execute commands from a command prompt makes it one of the best modules available. Developers and system administrators often use shell scripts to automate routine tasks such as maintaining data backups and managing system resources. However, as tasks become more complex, shell scripts can be challenging to maintain. Luckily, Python offers an alternative to shell scripts for automation.
Python enables you to perform most of the automation tasks that would otherwise require manual intervention or separate scripts by executing shell commands. With Python, we can leverage the functionality of the command line interface (CLI) within a Python script and achieve the same results as shell scripts. In this tutorial, we will learn how to execute a shell command in Python so that we can automate computer tasks in a more organized and scalable way.
If you want to learn more about Python Programming, visit Python Programming Tutorials.
What is a shell?
On Windows systems, shell environments are commonly referred to as command prompts or terminals. To open the command prompt, click the Windows icon button and press Win+R. Then, type cmd and hit Enter. On Linux, you can open a terminal by pressing the keyboard combination Ctrl+Alt+T. If you’re using Mac OS, you can press Cmd+Space, type terminal, and hit Enter to use shell commands. Many data scientists, programmers, and system engineers use shell commands for various tasks such as:
- Navigate and manipulate the file system, such as creating directories or deleting files
- Start and stop processes, such as starting a web server or stopping a database
- Manage system resources, such as monitoring memory usage or CPU utilization
What is the purpose of using Python for shell?
Python can be useful for executing shell commands for several reasons. Firstly, it provides an easy and user-friendly interface to interact with the operating system and import convenient modules, such as sys, os, subprocess, glob, platform, and shutils. Additionally, if you need to execute an external file such as a .exe or any application, you can use these modules.
Using Python, developers have more power and control, allowing for workflow automation. Tasks such as disk scanning, deleting junk cache files, backup, recovery, and other repetitive tasks can be implemented using Python shell scripts.
Methods For Executing a shell command in Python
This article demonstrates different ways to execute a shell command in Python. Some of them are listed below:
- os.system()
- subprocess.run()
- subprocess.Popen()
Executing Shell Commands using the os module
The easiest and most common method of executing shell commands in Python is by using the os.system() method. The os module in Python can be used to interact with operating systems. The os module is one of the most popular features of Python. The system() method in the os module can also be used to execute shell commands based on the features of the operating system.
Here, we will be using the system() method for executing a shell script of file “smillyface.py” stored in the DATA_SCIENCE\python directory.
# Importing os module to execute shell command
import os
os.system(r"D:\DATA_SCIENCE\python\smillyface.py")
0
when executing the above command the following window will open.
open with specified application.
Executing Shell Commands using the subprocess module
In Python, you can use the subprocess module to execute shell commands. The subprocess.Popen() function is the approachable way to execute the subprocesses. However, using subprocess.Popen() method to execute a shell command in Python.
Python allows us to use the built-in subprocess.Popen() function to execute the shell command in Python script. If you use shell=True with the Popen function, the command will execute in a separate recommended file shell.
# Importing required module
import subprocess
# Using system() method to
# execute shell commands
i=subprocess.Popen('parts of flowers.csv', shell=True)
stdout, stderr = i.communicate()
print(stdout, stderr)
Here is the provided csv file, when executed under subprocess, opens in an excel sheet.
subprocess.run()
Run shell scripts in Python by using the system() function. However, use the run() to execute shell scripts. This is a much faster and more flexible method than popen().
A sequence of arguments always begins with the first item in args (argument should be list or a string), which is the program to be executed. It is important to pass arguments to the command.
# Importing required module
import subprocess
# Using system() method to
# execute shell commands
i=subprocess.run(["parts of flowers.csv"], shell=True)
print(i)
CompletedProcess(args=['parts of flowers.csv'], returncode=0)
The i.stdout attribute contains the output of the command as a bytes object, which you can decode to a string if desired.
Execute a Shell Command with Python using the subprocess.run() by setting capture_output= True.
In Python, you can use the subprocess module to execute shell commands. The subprocess.run() function is the recommended way to run subprocesses.
import subprocess
result = subprocess.run(['parts of flowers.csv'], shell=True, capture_output=True)
print(result)
CompletedProcess(args=['parts of flowers.csv'], returncode=0, stdout=b'', stderr=b'')
This runs the command [‘parts of flowers.csv’] which is the first argument passed to it and captures the output property command set to True. The result is appended as result.stdout attribute contains the output of the command as a bytes object, which you can decode to a string if desired.
Conclusion
On this page. There will be a demonstration of how you can execute a shell command in Python. There are three methods for executing the shell commands in Python with examples. The three methods to execute a shell command in Python are os.system(), subprocess.run(), and subprocess.Popen().