From: http://stackoverflow.com/questions/89228/how-to-call-external-command-in-python
Here's a summary of the ways to call external programs and the advantages and disadvantages of each:
os.system("some_command with args")
Passes
Command and arguments to your system's shell. This is nice because you
Can actually run multiple commands at once in this manner and set up
Pipes and input/output redirection. For example,
os.system("some_command < input_file | another_command > output_file")
However, while this is convenient, you have to manually handle
Escaping of shell characters such as spaces, etc. On the other hand,
This also lets you run commands which are simply shell commands and not
Actually external programs.
Http://docs.python.org/lib/os-process.html
stream = os.popen("some_command with args")
Will do the same thingos.system
Could t that it gives you a file-like object that you can use to access
Standard Input/Output for that process. There are 3 other variants
Popen that all handle the I/O slightly differently. If you pass
Everything as a string, then your command is passed to the shell; if you
Pass them as a list then you don't need to worry about escaping
Anything.
Http://docs.python.org/lib/os-newstreams.html
ThePopen
Class ofsubprocess
Module. This is intended as a replacementos.popen
But has the downside of being slightly more complicated by using UE of being so comprehensive. For example, you 'd say
print Popen("echo Hello World", stdout=PIPE, shell=True).stdout.read()
Instead
print os.popen("echo Hello World").read()
But it is nice to have all of the options there in one uniied class instead of 4 different popen functions.
Http://docs.python.org/lib/node528.html
Thecall
Function fromsubprocess
Module. This is basically just likePopen
Class and takes all of the same arguments, but it simply wait until
Command completes and gives you the return code. For example:
return_code = call("echo Hello World", shell=True)
Http://docs.python.org/lib/node529.html
The OS module also has all of the fork/exec/spawn functions that
You 'd have in a C program, but I don't recommend using them directly.
I have also used the output after the commands. getoutput ("ls") command is executed.