Note: The following uses the OS module to invoke external commands, but the module is deprecated, and usually we use the Subpross module
Os.system (): Result of command output on terminal, not captured
In [1]: import osin [2]: Os.system ('ls/tmp/')12 . txt 1. txt 2. txt 3. test1 test2 # Here's the result output to the terminal out[20 # This returns the status code after the command is executed
Os.popen (): Returns 1 objects, standard output
in [4 out = Os.popen ('ls/tmp/') # receives standard output by variable in [5out. Read () # can be viewed using read (), ReadLine (), ReadLines (), and so on out[ 5'12.txt\n1.txt\n2.txt\n3.txt\ntest1\ntest2\n'
Os.popen2 (): Returns 2 objects, standard input, standard output, respectively
In [3]: stdin, stdout = Os.popen2 ('ls/tmp/' # We use the stdin to receive the standard input and use the STDOUT to receive the standard output in [4]: Stdout.read () # The standard output can be viewed through the Read () method out[4]:'12.txt\n1.txt\n2.txt\n3.txt\ntest1\ntest2\n'In [5]: Stdin.write ('Hello' #) can also be written to standard input in [using the Write () method. 6]: Stdin.close ()# Close () to save the write content
Os.popen3 (): Returns 3 objects, standard input, standard output, standard error output, respectively
In [7]: stdin, stdout, stderr = Os.popen3 ('ls/tmp/aaa')in[ 9]: Stderr.read () # View standard error output out[9'ls:cannot access/tmp/aaa: No such file or directory\n'
Python calls external command: OS module