If the return code is anything else than zero, it means that an error occurred. You can use subprocess.call return codes to determine the success of the command.Įvery process will return an exit code and you can do something with your script based on that code. It also gives you a way to cleanly integrate shell commands into your scripts while managing input/output in a standard way. With subprocess you can suppress the output, which is very handy when you want to run a system call but are not interested about the standard output. Now, let’s move on and look at the Input / Output. “Invoking the system shell with shell=True can be a security hazard if combined with untrusted input” Note, the official Python documentation states a warning about using the shell=True argument. This time we set the shell argument to True subprocess.call('du -hs $HOME', shell=True) Let’s look at two examples where we show the summary of disk usage using subprocess.call() subprocess.call() Note, the default value of the shell argument is False. We can run the command line with the arguments passed as a list of strings (example 1) or by setting the shell argument to a True value (example 2) Let’s start looking into the different functions of subprocess. Subprocess intends to replace several other, older modules and functions, like: os.system, os.spawn*, os.popen*, popen2.* commands. The subprocess module allows us to spawn processes, connect to their input/output/error pipes, and obtain their return codes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |