Python subprocess (Windows, Linux and MacOS examples)

The subprocess module in Python allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This is useful for tasks such as running external commands, working with system processes, etc.

Here’s a tutorial on how to use the subprocess module:

Python subprocess on Linux Examples

1. Import the subprocess module:

import subprocess

2. Running a Simple Command:

You can use the subprocess.run() function to run a simple command. For example, to run the ls command in a Unix-like system:

result = subprocess.run(["ls", "-l"], stdout=subprocess.PIPE, text=True)
print(result.stdout)

In this example:

stdout=subprocess.PIPE captures the standard output of the command.
text=True indicates that the output should be treated as text (Python 3.5+).

3. Handling Input:

You can provide input to the subprocess using the input parameter:

result = subprocess.run(["grep", "search_term"], input="Some text to search\nAnother line to search", stdout=subprocess.PIPE, text=True)
print(result.stdout)

4. Handling Errors:

To check if the command executed successfully, you can inspect the returncode attribute of the result:

if result.returncode == 0:
    print("Command executed successfully")
else:
    print(f"Command failed with return code {result.returncode}")

5. Advanced Usage – Piping Output:

You can also chain commands together using pipes. For instance, to count the number of lines in the output of a command:

command1 = ["ls", "-l"]
command2 = ["wc", "-l"]

result1 = subprocess.run(command1, stdout=subprocess.PIPE, text=True)
result2 = subprocess.run(command2, input=result1.stdout, stdout=subprocess.PIPE, text=True)

print(result2.stdout)

6. Using Shell:

By default, subprocess.run() does not use a shell. If you need to run a command with shell features (e.g., using shell variables or wildcards), you can specify shell=True. However, be cautious with this option as it can introduce security risks if you’re not careful with user input.

result = subprocess.run("ls -l | grep search_term", shell=True, stdout=subprocess.PIPE, text=True)
print(result.stdout)

7. Handling Environment Variables:

You can set environment variables for the subprocess using the env parameter:

import os

custom_env = os.environ.copy()
custom_env["CUSTOM_VARIABLE"] = "custom_value"

result = subprocess.run(["echo", "$CUSTOM_VARIABLE"], stdout=subprocess.PIPE, text=True, env=custom_env)
print(result.stdout)

Remember to use environment variables securely and sanitize inputs to avoid security vulnerabilities.

Python Subprocess on Windows Examples

The subprocess module in Python works similarly on Windows as it does on Unix/Linux/Ubuntu systems. Here’s how you can perform the same steps on a Windows system:

1. Import the subprocess module:

import subprocess

2. Running a Simple Command:

You can use the subprocess.run() function to run a simple command. For example, to run the dir command to list files in a directory:

result = subprocess.run(["dir"], stdout=subprocess.PIPE, text=True, shell=True)
print(result.stdout)

In this example:

stdout=subprocess.PIPE captures the standard output of the command.
text=True indicates that the output should be treated as text (Python 3.5+).
shell=True is used to run the command in a shell.

3. Handling Input:

You can provide input to the subprocess using the input parameter:

result = subprocess.run(["findstr", "search_term"], input="Some text to search\nAnother line to search", stdout=subprocess.PIPE, text=True, shell=True)
print(result.stdout)

4. Handling Errors:

To check if the command executed successfully, you can inspect the returncode attribute of the result:

if result.returncode == 0:
    print("Command executed successfully")
else:
    print(f"Command failed with return code {result.returncode}")

5. Advanced Usage – Piping Output:

You can chain commands together using pipes, just like on Unix-like systems. For instance, to count the number of lines in the output of a command:

command1 = ["dir"]
command2 = ["find", "/c", "/v", "\"\""]

result1 = subprocess.run(command1, stdout=subprocess.PIPE, text=True, shell=True)
result2 = subprocess.run(command2, input=result1.stdout, stdout=subprocess.PIPE, text=True, shell=True)

print(result2.stdout)

6. Handling Environment Variables:

You can set environment variables for the subprocess using the env parameter:

import os

custom_env = os.environ.copy()
custom_env["CUSTOM_VARIABLE"] = "custom_value"

result = subprocess.run(["echo", "%CUSTOM_VARIABLE%"], stdout=subprocess.PIPE, text=True, shell=True, env=custom_env)
print(result.stdout)

Again, be cautious when using environment variables and shell features to avoid security risks.

These steps demonstrate how to use the subprocess module on Windows to run external commands and interact with system processes. Windows-specific commands like dir and findstr are used in these examples, but you can replace them with other Windows commands or any command-line tool you need to run.

Python subprocess on MacOs Examples

Using the subprocess module in Python on macOS is very similar to using it on Unix-like systems. Here are the same steps as before, adapted for macOS:

1. Import the subprocess module:

import subprocess

2. Running a Simple Command:

You can use the subprocess.run() function to run a simple command. For example, to run the ls command to list files in a directory:

result = subprocess.run(["ls", "-l"], stdout=subprocess.PIPE, text=True)
print(result.stdout)

In this example:

stdout=subprocess.PIPE captures the standard output of the command.
text=True indicates that the output should be treated as text (Python 3.5+).

3. Handling Input:

You can provide input to the subprocess using the input parameter:

result = subprocess.run(["grep", "search_term"], input="Some text to search\nAnother line to search", stdout=subprocess.PIPE, text=True)
print(result.stdout)

4. Handling Errors:

To check if the command executed successfully, you can inspect the returncode attribute of the result:

if result.returncode == 0:
    print("Command executed successfully")
else:
    print(f"Command failed with return code {result.returncode}")

5. Advanced Usage – Piping Output:

You can chain commands together using pipes, just like on Unix-like systems. For instance, to count the number of lines in the output of a command:

command1 = ["ls", "-l"]
command2 = ["wc", "-l"]

result1 = subprocess.run(command1, stdout=subprocess.PIPE, text=True)
result2 = subprocess.run(command2, input=result1.stdout, stdout=subprocess.PIPE, text=True)

print(result2.stdout)

6. Using Shell:

By default, subprocess.run() does not use a shell. If you need to run a command with shell features (e.g., using shell variables or wildcards), you can specify shell=True . However, be cautious with this option as it can introduce security risks if you’re not careful with user input.

result = subprocess.run("ls -l | grep search_term", shell=True, stdout=subprocess.PIPE, text=True)
print(result.stdout)

7. Handling Environment Variables:

You can set environment variables for the subprocess using the env parameter:

import os

custom_env = os.environ.copy()
custom_env["CUSTOM_VARIABLE"] = "custom_value"

result = subprocess.run(["echo", "$CUSTOM_VARIABLE"], stdout=subprocess.PIPE, text=True, env=custom_env)
print(result.stdout)

These steps demonstrate how to use the subprocess module on macOS to run external commands and interact with system processes. You can replace the commands in these examples with other macOS-specific commands or any command-line tool you need to run.

These are the basics of using the subprocess module in Python. It’s a powerful tool for interacting with the system and running external processes within your Python scripts.

Be sure to check the official Python documentation for more details and options: https://docs.python.org/3/library/subprocess.html