Run command line commands with Python
Computers come with a program called terminal or command prompt in which the user can write command line commands. When such a command is executed the computer will perform a task.
Some examples of tasks that a user can start with these commands:
- running a Python script
- installing a Python package
- creating a virtual environment for Python
- making a file or directory
- outputting the contents of a directory
- starting the computer program Visual Studio Code

In this article we will look at how you can run command line commands from within Python scripts. This way you don’t have to run the commands manually but can automate them.
Just 2 notes before we start:
- Be careful when using user input to automate commands. Users with bad intentions could send input to sabotage your system.
- Different operating systems have different commands to perform tasks. The commands that I show as examples might only work on Windows. But the way we run the commands should also work on other operating systems.
Importing the subprocess module
We will import the subprocess module that Python offers so that we can use its run function. To import it, just write: import subprocess
Running a command with subprocess.run
We will now run our first command. And we will use the run function for that. The command that we will write is the dir command. The linux equivalent is ls.
This command outputs the contents of a directory. As an extra argument we pass the path of the directory from which we want to see the contents.
import subprocess
command = 'dir "C:/Users/BE/Documents/Python Project"'
subprocess.run(command, shell=True)When I use the run function I always pass along the shell=True argument. This makes it possible to write the command as 1 string and is also necessary if you are on Windows.
The function prints the output automatically:
Volume in drive C is Windows
Volume Serial Number is XXXXXXX
Directory of C:\Users\BE\Documents\Python Project
20-02-2023 09:28 <DIR> .
20-02-2023 11:29 <DIR> ..
20-02-2023 09:37 106 help_functions.py
20-02-2023 09:37 121 main.py
20-02-2023 09:28 <DIR> __pycache__
2 File(s) 227 bytes
3 Dir(s) 850.703.937.536 bytes freeCapturing a command’s output
To assign the command’s output to a variable we can put the call of the run function in a variable assignment like output_object = subprocess.run(..).
But we also have to add the capture_output=True argument to our function call.
And finally, to make sure the output is stored as a string instead of bytes we pass text=True in our functin call.
Then our function call looks like this:
output_object = subprocess.run(command, shell=True,
text=True, capture_output=True)After running the code the variable output_object will hold a CompletedProcess instance. From this instance we can use 3 interesting attributes.
First, returncode, this has value 0 if there was no error with running the command.
Second, stdout, if no error occurred this attribute will have the text output that was printed in the previous example.
And third, stderr, if an error occurred this attribute will have some text about what went wrong.
Here is a full code example for capturing the output of a command:
import subprocess
command = 'dir "C:/Users/BE/Documents/Python Project"'
output_object = subprocess.run(command, shell=True,
text=True, capture_output=True)
if output_object.returncode == 0:
print(output_object.stdout)
else:
print(output_object.stderr)Running commands from a different directory
When using a Python program to run commands, by default they are run from the current working directory. That is the directory where the Python script is located.
For example, if we did not pass the "C:/Users/BE/Documents/Python Project" argument to the dir command, it would have shown the contents of the Python script’s directory.
But we can also run commands as if we were in a different directory. To do that, pass the cwd argument to the run function and specify the path of the directory. It can be either an absolute filepath or a releative filepath.
To learn more about filepaths in Python you can read this short article of mine:
An example of passing a relative filepath as current working directory to the subprocess.run function:
import subprocess
command = 'dir'
output_object = subprocess.run(command, shell=True,
text=True, capture_output=True,
cwd='Python Project')
if output_object.returncode == 0:
print(output_object.stdout)
else:
print(output_object.stderr)Thank you for reading!
You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:
