The Wayback Machine - https://web.archive.org/web/20240617181806/https://www.geeksforgeeks.org/executing-shell-commands-with-python/
Open In App

Executing Shell Commands with Python

Last Updated : 20 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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.

Python3




# 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.

Python3




# 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.

Python3




# 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.

Python3




# 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.

Python3




# Importing required module
import os
 
os.system('cat')


Output: 

 



Similar Reads

How to Execute Shell Commands in a Remote Machine in Python?
Running shell commands on a Remote machine is nothing but executing shell commands on another machine and as another user across a computer network. There will be a master machine from which command can be sent and one or more slave machines that execute the received commands. Getting Started We will be using Websocket protocol to send shell comman
3 min read
How to Execute Shell Commands in a Remote Machine using Python - Paramiko
Paramiko is a Python library that makes a connection with a remote device through SSh. Paramiko is using SSH2 as a replacement for SSL to make a secure connection between two devices. It also supports the SFTP client and server model. Authenticating SSH connection To authenticate an SSH connection, we need to set up a private RSA SSH key (not to be
4 min read
Executing functions with multiple arguments at a terminal in Python
Commandline arguments are arguments provided by the user at runtime and gets executed by the functions or methods in the program. Python provides multiple ways to deal with these types of arguments. The three most common are: Using sys.argv Using getopt module/li> Using argparse module The Python sys module allows access to command-line argument
4 min read
Executing SQL query with Psycopg2 in Python
In this article, we are going to see how to execute SQL queries in PostgreSQL using Psycopg2 in Python. Psycopg2 is a PostgreSQL database driver, it is used to perform operations on PostgreSQL using python, it is designed for multi-threaded applications. SQL queries are executed with psycopg2 with the help of the execute() method. It is used to Exe
2 min read
SQLAlchemy Core - Executing Expression
In this article, we are going to see how to execute SQLAlchemy core expression using Python. Creating table for demonstration: Import necessary functions from the SQLAlchemy package. Establish connection with the PostgreSQL database using create_engine() function as shown below, create a table called books with columns book_id and book_price. Inser
4 min read
Python | Execute and parse Linux commands
Prerequisite: Introduction to Linux Shell and Shell Scripting Linux is one of the most popular operating systems and is a common choice for developers. It is popular because it is open source, it's free and customizable, it is very robust and adaptable. An operating system mainly consists of two parts: The kernel and the Shell. The kernel basically
6 min read
WebDriver Navigational Commands forward() and backward() in Selenium with Python
Selenium is an effective device for controlling an internet browser through the program. It is purposeful for all browsers, works on all fundamental OS and its scripts are written in numerous languages i.e Python, Java, C#, etc, we can be running with Python. Requirement: You need to install chromedriver and set path. Click here to download.for mor
2 min read
Python Script to Shutdown your PC using Voice Commands
Yes, it is possible with the help of terminal and some modules in Python through which one can shut down a PC by just using voice commands Required Modules:OS module: It is an in-built module in python that provides function for interacting with the operating system.Speech Recognition module: It is an external module in python whose functionality d
6 min read
Menu driven Python program to execute Linux commands
Linux is one of the most popular operating systems and is a common choice for developers. However, it is difficult to remember the vast range of commands that Linux supports, and hence a Python program that can run these commands easily is demonstrated below. In this article, we will deal with a Python program that can be used to run complex Linux
3 min read
Python Turtle - Graphics Keyboard Commands
Python Turtle module is a graphical tool that can be used to draw simple graphics on the screen using a cursor. Python Turtle was a part of Logo programming language which had similar purpose of letting the users draw graphics on the screen with the help of simple commands. Turtle is a pre-installed module and has inbuilt commands and features that
3 min read
Practice Tags :