Executing Shell Commands with Python Last Updated : 09 Aug, 2024 Comments Improve Suggest changes Like Article Like 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.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 moduleThe 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. Python # 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. Python # 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 moduleThe 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: Comment More infoAdvertise with us Next Article Executing Shell Commands with Python riturajsaha Follow Improve Article Tags : Python python-utility python-os-module Practice Tags : python Similar Reads Run shell command from GUI using Python In this article, we are going to discuss how we can create a GUI window that will take Operating System commands as input and after executing them, displays the output in a pop-up window. Modules Required: PyAutoGui: It is a module in python which is used to automate the GUI and controls the mouse a 2 min read 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 wi 3 min read Command Line Scripts | Python Packaging How do we execute any script in Python? $ python do_something.py $ python do_something_with_args.py gfg vibhu Probably that's how you do it. If your answer was that you just click a button on your IDE to execute your Python code, just assume you were asked specifically how you do it on command line. 4 min read Using the Cat Command in Python The cat command is a Linux shell command. It is the shorthand for concatenate. It is placed among the most often used shell commands. It could be used for various purposes such as displaying the content of a file on the terminal, copying the contents of a given file to another given file, and both a 4 min read Automating some git commands with Python Git is a powerful version control system that developers widely use to manage their code. However, managing Git repositories can be a tedious task, especially when working with multiple branches and commits. Fortunately, Git's command-line interface can be automated using Python, making it easier to 3 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 par 6 min read Execute a String of Code in Python Sometimes, we encounter situations where a Python program needs to dynamically execute code stored as a string. We will explore different ways to execute these strings safely and efficiently.Using the exec function exec() function allows us to execute dynamically generated Python code stored in a st 2 min read Useful IPython magic commands In this article, we will cover IPython Magic commands/functions and discuss various types of magic commands available. In this article we will be using Jupyter Notebook to execute the magic commands first, we look at what are Magic functions and why we use them, then different types of magic functio 6 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, 4 min read Python | shutil.which() method Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Pythonâs standard utility modules. This module helps to automate the process of copying and removal of files and directories. shutil.which() method tells the path to an executab 2 min read Like