This Python script runs a shell command (ls -1
), captures its output, and displays information about the process execution.
import subprocess # Import the subprocess module
# Run the 'ls -1' command and capture the output
res = subprocess.run(['ls', '-1'], stdout=subprocess.PIPE)
# Print the return code of the process
print('returncode:', res.returncode)
# Print the number of bytes in stdout and the decoded output
print(' {} bytes in stdout:\n{}'.format(len(res.stdout), res.stdout.decode('utf-8')))
import subprocess
→ Imports thesubprocess
module to execute shell commands from Python.subprocess.run(['ls', '-1'], stdout=subprocess.PIPE)
- Executes the
ls -1
command in the shell. ls -1
lists files in the current directory, one per line.stdout=subprocess.PIPE
captures the command’s output.
- Executes the
res.returncode
→ Gets the exit code of the process (0 indicates success).len(res.stdout)
→ Finds the number of bytes in the output.res.stdout.decode('utf-8')
→ Decodes the output from bytes to a readable string.
If the current directory contains file1.txt
, file2.py
, and folder1/
, running the script would produce:
returncode: 0
24 bytes in stdout:
file1.txt
file2.py
folder1
- Save the script as
list_files.py
. - Open a terminal and navigate to the script’s directory.
- Run the script:
python list_files.py
- Linux/macOS →
ls
works by default. - Windows →
ls
is not a built-in command. Use:Or useres = subprocess.run(['dir'], shell=True, stdout=subprocess.PIPE)
os.listdir()
for a built-in alternative.
To handle errors and ensure compatibility, use:
import subprocess
try:
res = subprocess.run(['ls', '-1'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
print('returncode:', res.returncode)
print(' {} bytes in stdout:\n{}'.format(len(res.stdout), res.stdout.decode('utf-8')))
except subprocess.CalledProcessError as e:
print('Error:', e)