How to Execute System Command From Python – Detailed Guide

Executing system commands in python is helpful to automate repetitive system tasks such as file backup or health checks.

You can execute system commands from Python using subprocess.run([command]).

There are also other methods available to execute system commands from Python.

If you’re in Hurry

You can use the code snippet below to call a system command from Python.

Snippet

import subprocess

result = subprocess.run(['dir', 'F:\Writing\Files'], stdout=subprocess.PIPE, shell=True, text=True)

result.stdout

Output

Volume in drive F is New Volume
Volume Serial Number is 7CE4-C7CB

Directory of F:\\Writing\\Files

27-10-21  19:33    <DIR>          .
27-10-21  19:33    <DIR>          ..
27-10-21  19:33           335,042 commons-codec-1.11.jar
27-10-21  19:33            61,829 commons-logging-1.2.jar
4 File(s)      1,904,795 bytes
4 Dir(s)  50,031,755,264 bytes free'

If You Want to Understand Details, Read on…

This tutorial will teach you the different methods to execute a system command from Python.

Command Example

You’ll execute the Dir command in windows or ls command in Linux OS to list the folders and files in the directory for demonstration.

Dir Command in Windows

If you’re using Windows operating system, use the Dir command to list all the files and folders in the directory. It also accepts optional parameters to customise the output.

 c:\users\stackvidhya > dir

Output

All the files and folders in the directory stackvidhya will be listed.

Ls Command in Linux

If you’re using a Unix-like operating system, use ls to list all the files and folders in the directory. It also accepts optional parameters to customise the output.

 $ ls  

Output

 All the files and folders in the current directory will be listed.

Now, you’ll see how to execute the system or shell commands from Python using different options.

Using Subprocess.run

You can use the run() method in Subprocess to execute a shell command from Python. It is available in Python by default.

It executes the command as described in the arguments. It accepts one mandatory parameter as a list. As explained below, any additional arguments to the shell command itself can be added as a list item.

  • ['dir', 'F:\Writing\Files'] – Python list where dir is the shell command and 'F:\Writing\Files' is the additional argument to dir command to list the files in that specific directory.
  • stdout=subprocess.PIPEOptional. To capture the command output.
  • shell=TrueOptional. To execute the command over Python shell. This will be useful if you use Python just to execute the shell command for the enhanced control flow that Python offers over most system shells. Read the Security Considerations before using shell=True.
  • text=True – Optional. To encode stdin, stdout and stderr as text objects instead of binary streams.

Use the following command to execute the shell command dir and print the output in the console.

Snippet

import subprocess

result = subprocess.run(['dir', 'F:\Writing\Files'], stdout=subprocess.PIPE, shell=True, text=True)

result.stdout

You’ll see the details of files and folders available in the directory, as shown below.

Output

Volume in drive F is New Volume
Volume Serial Number is 7CE4-C7CB

Directory of F:\\Writing\\Files

27-10-21  19:33    <DIR>          .
27-10-21  19:33    <DIR>          ..
27-10-21  19:33           335,042 commons-codec-1.11.jar
27-10-21  19:33            61,829 commons-logging-1.2.jar
4 File(s)      1,904,795 bytes
4 Dir(s)  50,031,755,264 bytes free'

This is how you can call a shell command using subprocess.run().

Using Subprocess.popen

You can also use the popen constructor and communicate() method in Subprocess to execute a shell command from Python. It is available in Python by default.

Popen constructor accepts the same parameters as the run method.

After creating the Popen object, you should invoke the communicate() method, and it returns the tuple that consists stdout (Output messages) and stderr (error messages).

The messages are available in byte mode. Use the decode() method to decode it to String.

Snippet

import subprocess

p = subprocess.Popen(['dir', 'F:\Writing\Files'], shell=True, stdout=subprocess.PIPE)

stdout, stderr = p.communicate()

stdout.decode()

Output

Volume in drive F is New Volume
Volume Serial Number is 7CE4-C7CB

Directory of F:\\Writing\\Files

27-10-21  19:33    <DIR>          .
27-10-21  19:33    <DIR>          ..
27-10-21  19:33           335,042 commons-codec-1.11.jar
27-10-21  19:33            61,829 commons-logging-1.2.jar
4 File(s)      1,904,795 bytes
4 Dir(s)  50,031,755,264 bytes free'

This is how you can use the popen class, and the communicate() method to execute a shell command in Python.

Using OS.System

OS module helps interact with the operating systems. Be it Linux or Windows.

System command in the OS module helps you execute the command in the subshell.

Use the below code to execute a shell command using the os.system() method.

Snippet

import os

home_dir = os.system("cd ~")

print("`cd ~` ran with exit code %d" % home_dir)

unknown_dir = os.system("cd doesnotexist")

print("`cd doesnotexis` ran with exit code %d" % unknown_dir)

Output

    `cd ~` ran with exit code 1
    `cd doesnotexis` ran with exit code 1

This is how you can use the os.system command to execute the shell command.

Execute Shell Command and Get Output

Get output While using Subprocess.RUN

The following code demonstrates how to execute a system command and get output while using the subprocess.run().

Snippet

import subprocess

result = subprocess.run(['dir', 'F:\Writing\Files'], stdout=subprocess.PIPE, shell=True, text=True)

result.stdout

Output

Volume in drive F is New Volume
Volume Serial Number is 7CE4-C7CB

Directory of F:\\Writing\\Files

27-10-21  19:33    <DIR>          .
27-10-21  19:33    <DIR>          ..
27-10-21  19:33           335,042 commons-codec-1.11.jar
27-10-21  19:33            61,829 commons-logging-1.2.jar
4 File(s)      1,904,795 bytes
4 Dir(s)  50,031,755,264 bytes free'

Get output While using Subprocess.Popen

The following code demonstrates how to execute a system command and get output while using the subprocess.Popen().communicate().

import subprocess

p = subprocess.Popen(['dir', 'F:\Writing\Files'], shell=True, stdout=subprocess.PIPE)

stdout, stderr = p.communicate()

stdout.decode()

Output

Volume in drive F is New Volume
Volume Serial Number is 7CE4-C7CB

Directory of F:\\Writing\\Files

27-10-21  19:33    <DIR>          .
27-10-21  19:33    <DIR>          ..
27-10-21  19:33           335,042 commons-codec-1.11.jar
27-10-21  19:33            61,829 commons-logging-1.2.jar
4 File(s)      1,904,795 bytes
4 Dir(s)  50,031,755,264 bytes free'

Execute Shell Command and Get Error

Get Error While using Subprocess.RUN

The following code demonstrates how to execute a shell command and get errors using the subprocess.run().

In this example, the directory is not existing. hence you’ll see the output File Not Found

import subprocess

result = subprocess.run(['dir', 'F:\Writing\Filess'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, text=True)

result.stderr

Output

    'File Not Found\n'

Get Error While using Subprocess.Popen

The following code demonstrates how to execute a system command and get errors using the subprocess.Popen().communicate().

In this example, the directory is not existing. Hence you’ll see the output of File Not Found message.

Snippet

import subprocess

result = subprocess.Popen(['dir', 'F:\Writing\Filess'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

stdout, stderr = result.communicate()

stderr

Output

    'File Not Found\n'

This is how you can get output messages and error messages while executing system commands in Python.

Run Shell Script With Arguments

This section will teach you how to pass arguments while running a shell script from Python.

The arguments can be passed as additional items in the command’s list parameter.

For example, If you want to list only the directories in a folder using the dir command in Windows, you can do it with the additional argument /aD. Add this argument as an additional list item and pass the list to subprocess.run(). It will execute with the shell command with additional arguments appropriately.

Snippet

import subprocess

result = subprocess.run(['dir', 'F:\Writing\Files', '/aD'], capture_output=True, shell=True)

print(result.stdout.decode())

Only the directories in the folder F:\Writing\Files are listed below.

Output

     Volume in drive F is New Volume
     Volume Serial Number is 7CE4-C7CB

     Directory of F:\Writing\Files

    27-10-21  19:33    <DIR>          .
    27-10-21  19:33    <DIR>          ..
    27-10-21  19:31    <DIR>          src
    27-10-21  19:23    <DIR>          Web-Pagination-Finder-master
                   0 File(s)              0 bytes
                   4 Dir(s)  50,031,755,264 bytes free

This is how you can pass arguments to shell commands while running from Python.

Run Multiple Commands at Once

In this section, you’ll learn how to run multiple shell commands at once from Python.

In windows: You can use the & operator to concatenate two commands.

In Linux: You can use the | operator to concatenate two commands.

For example, you’ll use two commands dir F:\Writing\Files and echo 'All the files and folders are listed'

The first command will list the files and folders in the directory, and the second command will print the output All the files and folders are listed.

Snippet

import subprocess

command = "dir F:\Writing\Files & echo 'All the files and folders are listed'"

result= subprocess.run(command, stdout=subprocess.PIPE, shell=True)

print(result.stdout.decode())

Output

Volume in drive F is New Volume
Volume Serial Number is 7CE4-C7CB

Directory of F:\\Writing\\Files

27-10-21  19:33    <DIR>          .
27-10-21  19:33    <DIR>          ..
27-10-21  19:33           335,042 commons-codec-1.11.jar
27-10-21  19:33            61,829 commons-logging-1.2.jar
4 File(s)      1,904,795 bytes
4 Dir(s)  50,031,755,264 bytes free'
'All the files and folders are listed'

Conclusion

To summarise, you’ve learned how to execute a shell command in Python.
Also, you’ve learned how to pass arguments, run multiple shell commands at once, and get the output and error messages while calling shell commands from Python.

If you’ve any questions, comment below.

You May Also Like

Python List To String – Definitive Guide

Leave a Comment