Top 10 Python Subprocess Programs
You can use Python to write shell scripts by using the subprocess module to call shell commands, as well as the shlex module to parse shell commands.
Here's an example of how you might use Python to reverse the lines in a file using the tac command:
import subprocess import shlex # Open the file and read in the lines with open('file.txt', 'r') as f: lines = f.readlines() # Reverse the lines lines = reversed(lines) # Write the reversed lines back to the file with open('file.txt', 'w') as f: for line in lines: f.write(line) # Use the `tac` command to reverse the lines in the file command = 'tac file.txt' subprocess.call(shlex.split(command))
This script opens a file, reads in all of the lines, reverses them, and then writes them back to the file. It then uses the tac command to reverse the lines again.
You can use a similar approach to call other shell commands from within a Python script. Just make sure to use the subprocess module and the shlex module to properly parse and execute the commands.
python top 10 subprocess programing
- Executing a simple shell command:
- Capturing the output of a shell command:
- Executing a shell command and passing command-line arguments:
- Executing a shell command and capturing its exit code:
- Executing a shell command and redirecting its output to a file:
- Executing a shell command and sending input to it via stdin:
- Executing a shell command and suppressing its output:
- Executing a shell command in the background:
- Executing a shell command and waiting for it to complete:
- Run a command and store its output in a variable:
import subprocess
output = subprocess.run(["ls", "-l"], capture_output=True).stdout
print(output)
- Run a command and check its exit status:
import subprocess
try:
subprocess.run(["false"], check=True)
except subprocess.CalledProcessError as err:
print("Command failed with exit status", err.returncode)
- Run a command with input supplied through stdin:
import subprocess
p = subprocess.run(["wc"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write(b"Hello, world!\n")
p.stdin.close()
print(p.stdout)
- Run a command and redirect its output to a file:
import subprocess
with open("output.txt", "w") as f:
subprocess.run(["ls", "-l"], stdout=f)
- Run a command and specify the working directory:
import subprocess
subprocess.run(["git", "clone", "https://github.com/user/repo"], cwd="/path/to/directory")
- Run a command and hide its output:
import subprocess
FNULL = open(os.devnull, "w")
subprocess.run(["command"], stdout=FNULL, stderr=FNULL)
- Run a command and capture its output and error streams separately:
import subprocess
p = subprocess.run(["command"], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
print(p.stdout)
print(p.stderr)
- Run a command and pass environment variables to it:
import subprocess
env = os.environ.copy()
env["VARNAME"] = "varvalue"
subprocess.run(["command"], env=env)
- Run a command and get its output as a string:
import subprocess
output = subprocess.run(["command"], capture_output=True, text=True).stdout
print(output)
- Run a command and get its output as a list of lines:
import subprocess
output = subprocess.run(["command"], capture_output=True, text=True).stdout
lines = output.strip().split("\n")
print(lines)
0 Comments