Skip to content

Latest commit

 

History

History
78 lines (60 loc) · 2.24 KB

File metadata and controls

78 lines (60 loc) · 2.24 KB

This Python script runs a shell command (ls -1), captures its output, and displays information about the process execution.


Breakdown of the Code

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')))

How It Works

  1. import subprocess → Imports the subprocess module to execute shell commands from Python.
  2. 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.
  3. res.returncode → Gets the exit code of the process (0 indicates success).
  4. len(res.stdout) → Finds the number of bytes in the output.
  5. res.stdout.decode('utf-8') → Decodes the output from bytes to a readable string.

Example Output

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

How to Run the Script

  1. Save the script as list_files.py.
  2. Open a terminal and navigate to the script’s directory.
  3. Run the script:
    python list_files.py

Cross-Platform Compatibility

  • Linux/macOSls works by default.
  • Windowsls is not a built-in command. Use:
    res = subprocess.run(['dir'], shell=True, stdout=subprocess.PIPE)
    Or use os.listdir() for a built-in alternative.

Improved Version with Error Handling

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)