
Os.fork, os.exec, os.spawn are similar to their C language counterparts, but I don't recommend using them directly. Similar to the above but even more flexible and returns a CompletedProcess object when the command finishes executing. For example: return_code = subprocess.call("echo Hello World", shell=True) This is basically just like the Popen class and takes all of the same arguments, but it simply waits until the command completes and gives you the return code. Instead of print os.popen("echo Hello World").read()īut it is nice to have all of the options there in one unified class instead of 4 different popen functions. For example, you'd say: print subprocess.Popen("echo Hello World", shell=True, stdout=subprocess.PIPE).stdout.read() This is intended as a replacement for os.popen, but has the downside of being slightly more complicated by virtue of being so comprehensive. 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. There are 3 other variants of popen that all handle the i/o slightly differently. Os.popen will do the same thing as os.system except that it gives you a file-like object that you can use to access standard input/output for that process. On the other hand, this also lets you run commands which are simply shell commands and not actually external programs.

However, while this is convenient, you have to manually handle the escaping of shell characters such as spaces, et cetera.

For example: os.system("some_command output_file") This is nice because you can actually run multiple commands at once in this manner and set up pipes and input/output redirection. Os.system passes the command and arguments to your system's shell. Here is a summary of ways to call external programs, including their advantages and disadvantages:
