Python calls Shell scripts in two ways: OS. system (cmd) or OS. popen (cmd), the former returns the exit code of the script, and the latter returns the output content during script execution. So we generally think popen is more powerful.
OS. system (cmd ):
After the shell script is called, this method returns a 16-bit binary number. The low position is the signal number of the script to be called, and the high position is the exit status code of the script, that is, after the code of exit 1 in the script is executed, the OS. the returned value of the system function is 1. If the returned value is 0, the return value of the function is 0 × 100. If the returned value is 0, the return value is 256.
OS. popen (cmd ):
This call method is implemented through pipelines. The function returns a file-like object with the content output by the script (which can be simply understood as the echo output content ).
Example:
1 import numpy2 import os3 t = OS. popen ('LS') 4 print t. read () 5 t = OS. system ('LS') 6 print t
Effect:
Server. py
Setup. py
T
Test
Test. c
Test. nja
Test. o
0