January 2019
Beginner
318 pages
8h 23m
English
In this section, we are going to learn about how we can capture output. We will pass PIPE for the stdout argument to capture the output. Write a script called capture_output.py and write the following code in it:
import subprocessres = subprocess.run(['ls', '-1'], stdout=subprocess.PIPE,)print('returncode:', res.returncode)print(' {} bytes in stdout:\n{}'.format(len(res.stdout), res.stdout.decode('utf-8')))
Execute the script as follows:
student@ubuntu:~$ python3 capture_output.py
On execution, we will receive the following output:
Output:returncode: 0191 bytes in stdout:1.pyaccept_by_input_file.pyaccept_by_pipe.pyexecute_external_commands.pygetpass_example.pyouput.txtoutput.txtpassword_prompt_again.py ...
Read now
Unlock full access