Javatpoint Logo
Javatpoint Logo

Python Subprocess Run

Python is the most powerful programming language. It offers a wide range of tools and libraries for various tasks. One of the main tools is the subprocess module. It allows the programmer to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. The subprocess module comes in handy when the programmer wants to execute system commands or external scripts from within a Python program. In this article, we are going to explore the Python subprocess run function in detail and explain how it can be used with examples.

Introduction to Subprocess Module

Before we learn about the details of the subprocess run function, let's first understand what the subprocess module is and what it offers. The subprocess module is part of the Python standard library, providing a way to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module allows the programmer to execute external programs, shell commands, and scripts from within a Python program.

The subprocess module offers several functions, including call(), check_call(), check_output(), Popen(), and run(). Each of these functions has its own use cases and features. However, the run() function is the recommended way to run external commands and scripts in Python 3.5 or later versions.

What is the Subprocess Run Function?

The subprocess run() function is a high-level function. It allows the programmer to run a command or script and capture its output, error, and return code. The run() function is a wrapper around the Popen() function, which creates a new process and connects to its input/output/error pipes.

The run() function takes several arguments, including the command to run, arguments, input, output, error, timeout, and more.

Syntax:

The syntax of the subprocess run() function is as below.

Let's understand each argument in detail:

  • args: This command is used to run and its arguments as a list or string.
  • stdin: This is used for the input pipe to connect to the process.
  • input: The input is used to send the process as a string.
  • stdout: The output pipe is used to connect the process.
  • stderr: The error pipe is used to connect the process.
  • capture_output: It is a boolean flag that specifies whether to capture the output and error of the process.
  • shell: It is a boolean flag that specifies whether to run the command in a shell.
  • cwd: It is the current working directory of the process.
  • timeout: It is the maximum time to wait for the process to complete.
  • check: It is a boolean flag that specifies whether to raise an error if the process returns a non-zero exit code.
  • encoding: The encoding is used for input, output, and error pipes.
  • errors: The error handling scheme is used for input, output, and error pipes.
  • text: It is a boolean flag that specifies whether to handle input, output, and error pipes as text or bytes.
  • env: The environment variables to set for the process.
  • universal_newlines: A boolean flag that specifies whether to use universal newlines for input, output, and error pipes.

Example usage of subprocess run()

Now that we understand the syntax and arguments of the subprocess run() function, let's see some examples of how it can be used in a Python program.

Example 1:

Output:

Python Subprocess Run

Explanation:

The output shows the total number of blocks used by the files in the directory (total 12), followed by the properties of each file. Each line contains the following information:

  • File permissions (-rw-r--r--): It indicates who can read, write, and execute the file.
  • A number of links (1): It indicates the number of hard links to the file.
  • The owner (user): It Indicates the owner of the file.
  • Group (user): It Indicates the group that owns the file.
  • File size (18, 112, 22): It Indicates the size of each file in bytes.
  • Last modification time (Apr 16 16:12, Apr 16 16:13, Apr 16 16:14): It Indicates the time when each file was last modified.
  • File name (file1.txt, file2.txt, file3.txt): It Indicates the name of each file in the directory.

So the exact output will depend on the files present in the current directory, their properties, and the order in which they are listed by the ls -l command.

Example 2:

Output:

5

Explanation:

In the above code, the program takes two arguments, 2 and 3, adds them together, and returns the result, which is 5. The result.stdout attribute contains the output of the script as a string, which is "5\n". The print() statement in the code above will print this string to the console. The \n character at the end of the string represents a newline character, which is added automatically by the print() statement.

Example 3:

Output:

Python Subprocess Run

Explanation:

The output of this code will be a list of all files in the current directory with a .py extension. The format of the output will be similar to the output of the ls -l command in a Unix/Linux terminal, but with only the files that match the grep .py filter.

Advantages

There are so many advantages of using the Python subprocess run() function for running external commands and scripts from within a Python program. These are as follows.

  1. Platform Independence: The subprocess module is a part of the Python standard library and works on all major operating systems, including Windows, Linux, and macOS.
  2. Flexibility: The subprocess run() function allows the programmer to execute a wide range of commands and scripts, including shell commands, Python scripts, and other executable programs.
  3. Input/Output Handling: The run() function provides input/output handling capabilities, allowing the programmer to capture the output and error of the process and send input to it.
  4. Security: The subprocess module provides a secure way to run external commands and scripts from within a Python program by preventing common security vulnerabilities, such as command injection attacks.
  5. Error Handling: The run() function provides error handling capabilities, allowing the programmer to check the return code of the process and raise an error if it returns a non-zero exit code.
  6. Integration: The subprocess module can be integrated with other Python modules, such as the os and sys modules, to provide more advanced functionalities, such as process management and communication.

Disadvantages:

There are also some advantages. These are as follows.

  1. Complexity: The subprocess module and it's run() function can be complex to use, especially for beginners. It requires a good understanding of command-line interfaces and system processes.
  2. Security Risks: While the subprocess module provides a secure way to run external commands and scripts, there are still potential security risks, especially if the commands or scripts are coming from untrusted sources.
  3. Performance Overhead: Running external commands and scripts through the subprocess module can introduce performance overhead, especially for large or resource-intensive programs.
  4. Lack of Interactivity: The run() function may not be suitable for interactive commands or scripts that require user input during execution.
  5. Limited Error Reporting: The run() function only provides basic error reporting capabilities, and it may not be sufficient for advanced error handling or debugging purposes.
  6. Compatibility Issues: The subprocess module may not be compatible with some older Python versions or third-party modules that rely on specific versions of the module.

Conclusion

In this article, we have learned about the Python subprocess module and the subprocess run() function. We have explored the syntax and arguments of the run() function and seen how it can be used to run commands, scripts, and shell commands from within a Python program.

A subprocess module is a powerful tool that allows the programmer to execute external programs and scripts from within your Python program and capture their output and return codes.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA