To run Python code itself, you need to use the Python interpreter. This is the software that converts Python code and executes it appropriately on your Windows PC. You’ll need to open a command line or PowerShell window to be able to use the interpreter. To open a PowerShell window, right-click the Windows Start menu and press Windows. Setup-python V2 What's new Usage Getting started with Python + Actions Available versions of Python Available versions of PyPy Hosted Tool Cache Specifying a Python version Specifying a PyPy version Using setup-python with a self hosted runner Windows Linux Mac Using Python without setup-python Using setup-python on GHES License Contributions. Python and associated Python scripts can be run using command-line interfaces. Windows users can use command prompt while Mac and Linux users can make use of Terminal. We'll cover how to run a Python script, open a Python shell, and how to run a Python one-liner.
Welcome to Python Run Shell Command On Windows tutorial. In this tutorial, you will learn, how to run shell command in python. So let’s move forward.
Python provides lots of modules for executing different operations related to operating system.
Generally there are two important modules which are used to run shell command in python.
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions:
The subprocess module allows users to communicate from their Python script to a terminal like bash or cmd.exe.
Now we will see different functions of subprocess module.
call() method create a separate process and run provided command in this process.
Write the following code to implement call() method of subprocess module.
2 4 | importsubprocess |
In this example, we will create a process for dir command
It’s output will be as follows.
check_output() Unsupported macos versions. is used to capture the output for later processing. So let’s see how it works.
2 4 | importsubprocess print(output) |
Output
The OS module is used to interact with underlying operating system in several different ways.
OS is an python built-in module so we don’t need to install any third party module. The os module allows platform independent programming by providing abstract methods.
The most straight forward approach to run a shell command is by using os.system().
2 4 | importos |
The problem with this approach is in its inflexibility since you can’t even get the resulting output as a variable. os.system() doesn’t return the result of the called shell commands. So if you want to capture output then you have to use os.popen() method.
The os.popen() command opens a pipe from or to the command line. This means that we can access the stream within Python. This is useful since you can now get the output as a variable. Now let’s see it practically, so write the following code.
2 4 | importos print(output) |
Now it’s output will be as follows.
So guys, that’s it for Python Run Shell Command On Windows tutorial. I hope it is helpful for you and if you have any query regarding this post then ask your questions in comment section. Thanks Everyone.