One of the cool things about Python is that you can do absolutely anything and everything with it. This blog post deals with using command line within Python. When you are writing your code in Python, you might want to access the command line to run a couple of commands like ls, grep, make etc. If it were a shell script, you could directly write that command and it will get executed as if you were on the command line. But shell scripting will only provide limited functionalities. So how do we do it in Python?
How to run commands?
The function to be used in this situation is “system” in the os library.
import os os.system("ls -l")
We pass it as a string to this function. Passing the command line to the system function will cause the command to be executed in the shell and print it straight away to standard output. We don’t get to keep the output for further manipulation.
How to use the output from these command line calls?
The function to be used in this case is “popen”, also in the os library. It is the same as above, but it returns the result into a file object which allows us to manipulate it.
import os f = os.popen("ls -l") for line in f.readlines(): print line
Let’s consider a practical example here. If you want to download a bunch of text files from the internet and do some natural language processing on them, you can run wget using os.system and read the files using “open” in Python. Once you have the data, there are various libraries available in Python to do text processing.
In more recent versions of Python, they have introduced Subprocess management. The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This is supposed to replace the os.system, os.popen and a few other modules and functions. Subprocess management will provide you with a few more flexibilities. The usage is as shown below:
import subprocess subprocess.call(["ls", "-l"])
This is equivalent to os.system(“ls -l”). There are a lot more functionalities available in the subprocess module. Depending on your needs, you can look up the appropriate functions.