January 11, 2017

Python - Running Commands

If you want to run some shell commands from your python script. Often this is sometimes needed in your code and the way that this is done in python is with subprocess module. So to run a shell command using the subprocess module you import call.

from subprocess import call
 
After that you can use call to pass the arguments and call the shell command like the following.
call(["touch", "newfile.txt"])
 
That is all there is to it. More about how to use the subprocess module you can check the documentation.

Tags: Python Code Guide