Run Commands with Subprocess

Talk it through out loud with your AI coach.

The problem

  • medium
  • Pro

A script runs a command-line tool on a filename supplied by users, by building a command string and passing it to os system. Explain the risks and how you'd do it with the subprocess module instead, including handling failures and hangs.

What a strong answer covers

  • Why passing user input through a shell is dangerous.
  • Running a program with its arguments kept separate.
  • Noticing failures and collecting the command's output.
  • Protecting the script from a command that never returns.

Common mistake: Trying to make the shell version safe by stripping a few characters from the filename instead of not using a shell.

Knowing this is half of it. Saying it clearly out loud is the part the coach helps you practise.

Practise it out loud