Q51: How do you execute system commands using the 'subprocess' module?

The subprocess module executes external shell commands. Modern best practice uses subprocess.run(['cmd', 'arg'], capture_output=True, text=True, check=True) to prevent shell injection vulnerabilities.

Key Takeaways
  • Replaces legacy os.system calls.
  • Pass arguments as explicit list vectors (['ls', '-l']) to block shell injection attacks.
Knowledge Check

Why is passing command strings with shell=True considered a major security vulnerability?