How do I execute a program (or) system command using Python?


You can execute a program or system command using Python by using the subprocess module.


Read Also, Several ways to call external programs in Python


Here is an example of how to execute a system command:

import subprocess

# Run a program and pass arguments
subprocess.run(["echo", "Hello, World!"])

# Run a system command with shell expansion
subprocess.run("echo $PATH", shell=True)

In this example, the subprocess.run function is used to execute the echo program with the argument "Hello, World!". The subprocess.run function can also be used to run a system command with shell expansion. In this case, the echo $PATH command is executed, which prints the system’s PATH environment variable.


Here is another example:

import subprocess

# Run a system command and store the output in a variable
output = subprocess.check_output("ls -la", shell=True)

# Print the output
print(output.decode('utf-8'))

In this example, the subprocess.check_output function is used to execute the system command ls -la. The shell=True argument is used to run the command in a shell. The output of the command is stored in the output variable as a bytes object. The decode('utf-8') method is used to convert the bytes object to a string, which is then printed to the console.


Here is another example to run a command and get its output:

import subprocess

# Run the command and capture its output
result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE)

# Get the output as a string
output = result.stdout.decode('utf-8')

# Print the output
print(output)

In this example, we are running the ls -l command using the subprocess.run() method. We are using the stdout argument to capture the command’s output. We then use the decode() method to convert the byte string to a regular string, and then print the output to the console.


You can replace the command with any other system command or program that you want to run. Just replace the ['ls', '-l'] argument with the command you want to run and its arguments. The subprocess.run() method will return a CompletedProcess object, which contains information about the command’s execution, including its output. You can access the output using the stdout attribute of the CompletedProcess object.


Here is another example to run a command and get its output and error:

import subprocess

# Run the command and capture its output and error messages
result = subprocess.run(['ls', '-l', '/nonexistent'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# Get the output and error messages as strings
output = result.stdout.decode('utf-8')
errors = result.stderr.decode('utf-8')

# Print the output and error messages
print('Output:', output)
print('Errors:', errors)

In this example, we are running the ls -l /nonexistent command, which will generate an error message because the /nonexistent directory does not exist. We are using the stdout and stderr arguments to capture both the output and any error messages that might be generated.


We then use the decode() method to convert the byte strings to regular strings, and print the output and error messages to the console. If the command does not generate any errors, the errors variable will be an empty string.


You can replace the command with any other system command or program that you want to run. Just replace the ['ls', '-l', '/nonexistent'] argument with the command you want to run and its arguments. The subprocess.run() method will return a CompletedProcess object, which contains information about the command’s execution, including its output and any error messages. You can access the output and error messages using the stdout and stderr attributes of the CompletedProcess object.


Read Also, Several ways to call external programs in Python

Leave a Reply

Your email address will not be published. Required fields are marked *