|
This article summarizes the program execution system using the Python several commands, described the advantages and disadvantages of each module.
os.system module
Only in one sub-terminal run system commands, but can not get the return information after command execution, if executed at the command line, the results printed directly
Python 2.6.6 (r266: 84292, May 29 2014, 05:49:27)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> Import os
>>> Os.system ( 'cat cont')
biz_order_id
out_order_id
buy_nick
sel_nick
buyer
sel
0
>>> Out = os.system ( 'cat cont')
biz_order_id
out_order_id
buy_nick
sel_nick
buyer
sel
>>>
Disadvantages:
a.os.system () is starting a new shell command execution
b. can not use other variables to store os.system () implementation of the results, access to information such as the output is too much trouble.
c. can not control the status of the command execution, (if the external command invoked linked to death or executed a long time), the main process can not control os.system (), because the call os.system (cmd) the calling process will block, until os .system () command to end their own exit.
os.popen module
By os.popen () returns the file read object, an operation has read read () can see the output execution.
Example 1
>>> Output = os.popen ( 'date')
>>> Print output
>>> Print output.read ()
Sun Sep 13 22:34:56 CST 2015
Example 2
>>> Output = os.popen ( 'cat cont'). Readlines () ## returns an array
>>> Print output
[ 'Biz_order_id \ n', 'out_order_id \ n', 'buy_nick \ n', 'sel_nick \ n', 'buyer \ n', 'sel \ n']
>>> Print output [0]
biz_order_id
advantage
1 This method not only execute the command also returns information on the object after returning the result of a variable assigned to facilitate the processing of the program.
Shortcoming
1 Could not get the results of the command of the state.
commands module
And implementation modalities os.system similar to the terminal subsystem execute the command. The module contains the following three functions:
commands.getstatusoutput (cmd)
Results of commands.getoutput (cmd) command returns
commands.getstatus (file) returns the results of the implementation of ls -ld file.
>>> Output = commands.getoutput ( "date")
>>> Print output
Sun Sep 13 22:37:44 CST 2015
>>> Status = commands.getstatus ( "date")
>>> Print status
ls: can not access date: No such file or directory
>>> Commands.getstatus ( '/ bin / ls')
'-rwxr-Xr-x 1 root root 111744 May 29 2014 / bin / ls'
>>> St = commands.getstatus ( '/ bin / ls')
>>> Print st
-rwxr-xr-x 1 root root 111744 May 29 2014 / bin / ls
>>> Status, output = commands.getstatusoutput ( "ls yy")
>>> Print status, output
512 ls: can not access yy: No such file or directory
Advantages:
Status a. Easy access to external commands and command execution result output
Disadvantages:
b. can not use other variables to store os.system () implementation of the results, access to information such as the output is too much trouble.
c. can not control the status of the command execution, (if the external command invoked linked to death or executed a long time), the main process can not control os.system (), because the call os.system (cmd) the calling process will block, until os .system () command to end their own exit.
Note thing is that the module has been deprecated in python 3, it is recommended to use subprocess instead.
subprocess module
Run python, we are to create and run a process. Like Linux process as a process you can fork a child process, and let the child process exec another program. In Python, by the standard library subprocess package to fork a child process, and to run an external program. For details, please refer to the official documentation
>>> Import subprocess
>>> Subprocess.call ([ "ls", "-l"])
total 20
-rwxr-xr-x 1 qilong.yangql users 14 Dec 30 2014 a
-rw-r - r-- 1 qilong.yangql users 54 Sep 13 22:22 cont
-rwxr-xr-x 1 root root 19 Jun 1 11:01 d
-rw-r - r-- 1 root root 68 Jul 7 09:45 database
drwxr-xr-x 3 qilong.yangql users 4096 Jan 11 2015 dbscripts
0
>>> Subprocess.call ([ "ls"])
a cont d database dbscripts
0
>>> Popen = subprocess.Popen ([ "date"])
>>> Sun Sep 13 22:57:08 CST 2015
>>> Popen.pid
advantage
1 support and child interaction
2 Support command output results
3 You can use the kill (), terminate () to control the child process exits
4 There are many refer to the official documentation. |
|
|
|