Loading topic
git, ffmpeg, a compiler, or another Python script. os.system is the oldest way to do it and also the weakest. This chapter explains what os.system does, where it fails, and why the subprocess module is the standard replacement. Later chapte5 min readsubprocess.run: what it does, the object it returns, and the mistakes people most often make on a first attempt. It assumes you know basic Python syntax, functions and exceptions. It also assumes you can use a terminal, know that programs have standard input, standard output an5 min readsubprocess.run, read a CompletedProcess, and handle exceptions in Python. It also assumes you know that a command line is a program name followed by arguments. Here you will learn how subprocess turns your Python values into a command, and what changes when yo4 min readsubprocess.run and read a CompletedProcess. It also assumes you know what standard output and standard error are, and how Python's str differs from bytes. It covers how to get a child process's output into your program, what type that output has, and where d5 min readsubprocess.run, capture its output, and handle Python exceptions. It covers how to tell whether a child process succeeded and how to turn failure into an exception.4 min readsubprocess.run, return codes and check=True. You should be comfortable with Python exceptions and their class hierarchy.5 min readsubprocess.run, capture output, and handle exceptions such as CalledProcessError. It covers how to bound a command's run time, what the library does when the limit passes, and where that behavior stops short.4 min readsubprocess.run, capture its output with capture_output=True, and know the difference between str and bytes. It covers the input argument, the stdin argument, and what happens when the child process does not read what you send.5 min readsubprocess.run and read a CompletedProcess, and that you know what capture_output=True and check=True do. Here you will control two things a child process inherits from its parent: its environment variables and its current working directory.5 min readsubprocess.run with an argument list, you know what shell=True does, and you have read the earlier chapter on argument lists and shlex. You should also be comfortable reading a short shell command. The examples that use echo, grep and ; assume Linux or ma5 min readsubprocess.run, know what stdout, stderr and exit codes are, and understand the difference between text and bytes. subprocess.Popen is the class that run is built on. You need it when you must act while the child process is still running.5 min readsubprocess.run returns only after the child exits. For a build, a log tail, or a long job with progress messages, you often want each line the moment the child writes it. This chapter shows how to do that with Popen, and why lines sometimes arrive late or all at once.6 min readsubprocess.run, you know what Popen is, and you know that a child process has standard input, output and error streams. It shows how to connect the output of one program to the input of another from Python, the way a | b does in a shell. It then covers the plac5 min readsubprocess.run and Popen with stdout=subprocess.PIPE, and that you know what standard input, output and error are. It explains why a child process can freeze while your program waits for it, and how to prevent that.4 min readsubprocess.run, argument lists, shell=True, check=True, timeouts, and the Popen basics from the earlier chapters. The examples from those chapters mostly use sys.executable, so they run unchanged on Windows, macOS and Linux. Real programs also call tools like 6 min readsubprocess.run and Popen, including pipes, communicate, and why pipe buffers can deadlock. It also assumes you can read basic async and await code and know that asyncio.run starts an event loop. The chapter covers the asyncio versions of these tools, and whe5 min read