Python script that executes commands in the terminal

Python Scripts for Executing Commands in the Terminal

In this article, we’ll explain how to use Python scripts to execute commands in the terminal. Python, as a powerful programming language, is very convenient for handling system-level tasks and automating tasks. Whether in the Linux, macOS, or Windows terminal, you can use Python scripts to execute various commands.

Read more: Python Tutorial

Using the subprocess Module to Execute Commands

Python’s subprocess module is the preferred way to execute commands in the terminal. It provides many powerful methods for executing system commands and capturing command output and errors.

The following is a simple example demonstrating how to use the subprocess module to execute a simple command:

import subprocess

# Execute the command
result = subprocess.run(['ls', '-l'], capture_output=True, text=True)

# Print the command's return result
print(result.stdout)

In the above example, we use the subprocess.run() function to execute the ls -l command. The capture_output=True parameter captures the command’s output and stores its return result in result.stdout.

Calling External Commands

In addition to using the subprocess.run() function, Python provides several other ways to call external commands. As shown below:

  • subprocess.call(): Executes an external command and returns the command’s exit status.
  • subprocess.check_output(): Executes an external command and returns the command’s output.
  • subprocess.Popen(): Executes an external command in a more advanced way.

The following is an example using the subprocess.call() function:

import subprocess

# Execute command
exit_code = subprocess.call(['echo', 'Hello, World!'])

# Print the command's exit status
print(exit_code)

The above code will execute the command echo Hello, World! and print the command’s exit status.

Executing Complex Commands and Pipelines

Python’s subprocess module also supports executing complex commands and using pipelines. This can be achieved by using the pipe symbol | and the redirection symbol > in the command.

The following example demonstrates how to execute a complex command and redirect the output to a file:

import subprocess

# Execute a command and redirect the output to a file
subprocess.run(['ls', '-l', '|', 'grep', '.txt', '>', 'result.txt'], shell=True)

The above code executes the command ls -l | grep .txt > result.txt and redirects the command output to the file result.txt.

Executing Commands Using the os Module

In addition to using the subprocess module, Python’s os module can also be used to execute commands. The os module provides a series of functions for handling operating system-related functions.

The following is an example of using the os module to execute a command:

import os

# Execute Command
os.system('echo Hello, World!')

The above code will execute the command echo Hello, World!.

Note that the os.system() function prints the output directly after executing the command and does not return the output to the caller.

Summary

In this article, we introduced how to execute commands in the terminal using Python scripts. We learned different ways to execute commands using the subprocess module, including running simple commands, calling external commands, executing complex commands, and using pipes. We also briefly explored how to execute commands using the os module.

Python’s powerful capabilities make it an ideal language for executing commands in the terminal, making system-level and automated tasks more concise and efficient. I hope this article helps you better understand and apply Python’s command execution methods in the terminal.

For further information, please refer to the official Python documentation or other related resources. Happy learning!

Leave a Reply

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