The Python subprocess module allows to spawn new processes, execute external commands from Python scripts. You can install the latest version of Python using these tutorials. Also, there is number of IDE available for Python. Like Install PyCharm Python IDE on Ubuntu systems.
A Simple Python Example
Let’s create your first program to list all files available in the current directory. You can add any number of command line parameters with the comm (,) separated.
#!/usr/bin/python3 import subprocess subprocess.call(["ls", "-l"])
importstatement is used to load the subprocess module form the Python standard library callis the function of subprocess module, is used to execute external commands
Python Print Without New Line
Python commands terminate with new line output “\n”, which you can overwrite using
#!/usr/bin/python3 import subprocess print("\nToday is ", end="") subprocess.call(["date","+%D"])
Today is 01/11/18
Python Example with Shell Expansion
#!/usr/bin/python3 import subprocess # Executing command without shell expansion subprocess.call(['echo', 'Welcome $USER']) # Executing command with shell expansion subprocess.call('echo Welcome $USER', shell=True)
Welcome $USER Welcome root
You can see in above output that first command prints variable name because it was executed without shell expansion. The second command executed with shell expansion get the value of USER environment variable. Also, you can see that the entire command is now passed as a string and not as a list of strings.
Other Useful Python Examples
You can make your scripts more readable format and beautiful by storing a long command in a variable and then execute it.
#!/usr/bin/python3 import subprocess cmd = "grep -Re 'Fatal' /var/log/ > searches.txt" subprocess.call(cmd, shell=True)
You can also store the output of any command to a variable using
#!/usr/bin/python3 import subprocess print("The output of 'pwd' command is:", flush=True) output = subprocess.getoutput('pwd') print(output)
The output of 'pwd' command is: /root/Python/Scripts