Python provides the subprocess
module in order to spawn new processes and connect their input, output, and errors and get the return codes about the process. The subprocess is created to replace os.system
and os.spawn*
modules. The subprocess.check_output()
method is used to check the subprocess output.
subprocess.check_output() Syntax
The subprocess.check_output() method has the following complex syntax. But we can set and configure different features for the subprocess we run.
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, cwd=None, encoding=None, errors=None, universal_newlines=None, timeout=None, text=None, **other_popen_kwargs)
- args is a list that contains the subprocess arguments.
- stdin=None is used to specify the standard input. The default value is None.
- stderr=None is used to specify the standard output. The default value is None.
- shell=False is used to specify if a new shell will be created.
- cwd=None is used to set the current working directory.
- encoding=None is used to set encoding.
- errors=None
- universal_newlines=None
- timeout=None is used to set the timeout value if the subprocess does not complete executions and returns a result.
- text=None
Print subprocess.check_output
We can create a subprocess and return its output like below. The command and related parameters are provided as a list of the subprocess.check_output() method.
import subprocess
output = subprocess.check_output(['ls','-l','-a'])
print (output)
