A simple example Python
Let's create the first program to list all the available files in the current directory. You can add any number of comma (,) separated command line parameters.
#!/usr/bin/python3
import subprocess
subprocess.call(["ls", "-l"])
The import statement to load the sub-process module from the Python standard library
call sub-process is a functional module for performing external commands
Python prints with no line breaks
Python command-line output with new "\ n" terminate, you can end = "" covered by the new line of output, which means that the next command output on the same line. See the following example.
#!/usr/bin/python3
import subprocess
print("\nToday is ", end="")
subprocess.call(["date","+%D"])
Python sample having a Shell Extension Function
The default subprocess.call not use wildcards or shell extensions execute command substitution. This setting can be overridden by passing shell parameter value True. Remember to use the shell = True might be harmful because the execution order and security problems caused by the system.
#! / usr / bin / to python3
Import The subprocess
# Run shell without the use of extended
subprocess.call ([ 'echo', 'the USER available for purchase $'])
# Run using Shell extension
subprocess.call ( 'echo Welcome $ USER ', shell = True)
You can see in the above output, the first command to print a variable name, because it is performed in the absence of shell extensions circumstances. The second command using the shell extension performed to obtain the value of the USER environment variable. In addition, you can now see the entire command as a string instead of as a list of strings passed.
Other useful Python example
By then execute it in a variable length instruction memory, readability and aesthetic appearance can be made more scripts.
You can also use subprocess.getoutput stored in the variable output of any command. If you generate any error messages, it will be stored.
#!/usr/bin/python3
import subprocess
print("The output of 'pwd' command is:", flush=True)
output = subprocess.getoutput('pwd')
print(output)