Python exec system command

Python exec command

Python exec tutorial shows how to execute shell commands and programs in Python.

In Python, there are several ways to execute shell commands or programs. We can use the os module or the subprocess module.

The subprocess module has the most powerful tools for executing commands.

Python exec command with os.system

The os.system is a simple tool for executing a program.

#!/usr/bin/python import os os.system('firefox')

The example launches firefox.

Python exec command with os.popen

The os.popen allows us to receive input from the executed command. It opens a pipe to or from a command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is ‘r’ (default) or ‘w’.

#!/usr/bin/python import os output = os.popen('ls -la').read() print(output)

In the example, we read the output of the ls command and print it to the console.

$ ./read_output.py total 28 drwxr-xr-x 2 janbodnar janbodnar 4096 Oct 11 14:30 . drwxr-xr-x 113 janbodnar janbodnar 4096 Oct 11 13:35 .. -rwxr-xr-x 1 janbodnar janbodnar 202 Oct 11 14:02 listing.py -rwxr-xr-x 1 janbodnar janbodnar 218 Oct 11 13:54 node_version.py -rwxr-xr-x 1 janbodnar janbodnar 547 Oct 11 14:04 pinging.py -rwxr-xr-x 1 janbodnar janbodnar 78 Oct 11 13:39 read_output.py -rwxr-xr-x 1 janbodnar janbodnar 50 Oct 11 13:37 simple.py

Python exec command with subprocess

The subprocess module allows us 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.

#!/usr/bin/python import subprocess output = subprocess.run(['node', '--version'], text=True, capture_output=True) print(f'Node version: ')

The example uses subprocess module to get the version of Node.

output = subprocess.run(['node', '--version'], text=True, capture_output=True)

The run executes the command, waits for command to complete, and returns a CompletedProcess instance. To capture output to standard output and standard error output, we set the capture_output to true . By default, the output is returned in bytes string. By setting the text option to True , we get a normal string.

$ ./node_version.py Node version: v12.18.4

Python list contents with subprocess

In the following example, we list the contents of a user’s home directory.

#!/usr/bin/python import subprocess from pathlib import Path output = subprocess.Popen(['ls', '-l', Path.home()], text=True, stdout=subprocess.PIPE) stdout, _ = output.communicate() print(stdout)

The underlying process creation and management in this module is handled by the Popen class. It offers more flexibility than the convenience functions.

stdout, _ = output.communicate()

We interact with the process with the communicate method. It sends data to stdin and reads data from stdout and stderr, until EOF is reached.

In this tutorial we have executed shell commands and programs.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

Источник

How to Execute System Command From Python – Detailed Guide

Stack Vidhya

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.

import subprocess result = subprocess.run(['dir', 'F:\Writing\Files'], stdout=subprocess.PIPE, shell=True, text=True) result.stdout 
Volume in drive F is New Volume Volume Serial Number is 7CE4-C7CB Directory of F:\\Writing\\Files 27-10-21 19:33 . 27-10-21 19:33 .. 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.

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.

 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.PIPE – Optional . To capture the command output.
  • shell=True – Optional . 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.

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.

Volume in drive F is New Volume Volume Serial Number is 7CE4-C7CB Directory of F:\\Writing\\Files 27-10-21 19:33 . 27-10-21 19:33 .. 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 .

import subprocess p = subprocess.Popen(['dir', 'F:\Writing\Files'], shell=True, stdout=subprocess.PIPE) stdout, stderr = p.communicate() stdout.decode() 
Volume in drive F is New Volume Volume Serial Number is 7CE4-C7CB Directory of F:\\Writing\\Files 27-10-21 19:33 . 27-10-21 19:33 .. 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.

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)
 `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() .

import subprocess result = subprocess.run(['dir', 'F:\Writing\Files'], stdout=subprocess.PIPE, shell=True, text=True) result.stdout
Volume in drive F is New Volume Volume Serial Number is 7CE4-C7CB Directory of F:\\Writing\\Files 27-10-21 19:33 . 27-10-21 19:33 .. 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() 
Volume in drive F is New Volume Volume Serial Number is 7CE4-C7CB Directory of F:\\Writing\\Files 27-10-21 19:33 . 27-10-21 19:33 .. 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 

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.

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

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.

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.

 Volume in drive F is New Volume Volume Serial Number is 7CE4-C7CB Directory of F:\Writing\Files 27-10-21 19:33 . 27-10-21 19:33 .. 27-10-21 19:31 src 27-10-21 19:23 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.

import subprocess command result= subprocess.run(command, stdout=subprocess.PIPE, shell=True) print(result.stdout.decode())
Volume in drive F is New Volume Volume Serial Number is 7CE4-C7CB Directory of F:\\Writing\\Files 27-10-21 19:33 . 27-10-21 19:33 .. 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.

Источник

Читайте также:  Вывод даты
Оцените статью