Question:
Use of Python subprocess.run for calling other Python files and bash files

The subprocess.run() function in Python is a convenient way to execute external commands, including calling other Python files and Bash scripts from your Python script. It's a part of the subprocess module, which allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.

Here are examples of how to use subprocess.run() to call both Python files and Bash scripts:

Calling a Python File

Suppose you want to call another Python script (other_script.py) from your main Python script:

import subprocess # Define the command to run the other Python script command = ['python', 'other_script.py', 'arg1', 'arg2'] # Run the command result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) # Check the return code if result.returncode == 0: print("Other script ran successfully.") print("Output:") print(result.stdout) else: print("Other script encountered an error:") print("Error Output:") print(result.stderr)

In this example, replace 'other_script.py' with the actual filename of the Python script you want to run, and 'arg1', 'arg2', etc., with any command-line arguments that the script expects.

Calling a Bash Script

You can also use subprocess.run() to execute Bash scripts. For example, if you have a Bash script called my_script.sh:

#!/bin/bash echo "Hello from Bash script"

You can call it from your Python script like this:
import subprocess # Define the command to run the Bash script command = ['bash', 'my_script.sh'] # Run the command result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) # Check the return code if result.returncode == 0: print("Bash script ran successfully.") print("Output:") print(result.stdout) else: print("Bash script encountered an error:") print("Error Output:") print(result.stderr)

Replace 'my_script.sh' with the actual filename of your Bash script. Ensure that the Bash script has execute permissions (chmod +x my_script.sh) if you're running it directly.

Remember to handle errors and exceptions appropriately, depending on your use case. Additionally, use subprocess.run() with caution, as it can execute arbitrary code, and improper usage can pose security risks. Always validate and sanitize any user input used in command construction.

Solution:


import functools
import subprocess
import sys
from pathlib import Path

DIR_B_PATH = Path(__file__).parent.parent / "dirB"


execute_bash_command = functools.partial(
    subprocess.run,
    capture_output=True,
    text=True,
)


def call_app() -> subprocess.CompletedProcess:
    command = ["./app.sh", "install"]
    return execute_bash_command(command, cwd=DIR_B_PATH)


def call_utility() -> subprocess.CompletedProcess:
    command = [sys.executable, "utility.py", "greet"]
    return execute_bash_command(command, cwd=DIR_B_PATH)
Credit:> Stackoverflow

Answer by: >Hai Vu 




Ritu Singh

Ritu Singh

Submit
0 Answers