Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Use Python to perform system commands common usage     - Safe learning through self-extracting package to achieve invasion (Linux)

- Install Apache streaming media services on CentOS 6.4 (Server)

- Linux at command (Linux)

- How to install open source ITIL portal iTOP on CentOS 7 (Server)

- OpenGL shadow map (Programming)

- Netapp storage routine inspections and information gathering (Linux)

- Tomcat itself through simple movement separation (Server)

- Spring REST Exception Handling (Programming)

- Simple to use multi-threaded programming under Linux mutex and condition variable (Programming)

- C ++ Fundamentals study notes (Programming)

- CentOS 6.5 Telnet SecureCRT use management tools (Linux)

- Integrated security administrator Linux accident management (Linux)

- Java multi-threaded communications pipeline flow (Programming)

- How do you change the default browser and Email Client in Ubuntu (Linux)

- Using LLVM Clang and Blocks under Linux (Programming)

- Killall five options you might not know (Linux)

- Qt for file splitting and fusion gadgets (Programming)

- Ubuntu 64-bit installation Adobe Reader 9.5.5 (Linux)

- Intruder tools Knark Analysis and Prevention Linux environment (Linux)

- Based on Python: OpenCV simple image manipulation (Programming)

 
         
  Use Python to perform system commands common usage
     
  Add Date : 2018-11-21      
         
         
         
  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.
     
         
         
         
  More:      
 
- Ubuntu installation 2.10.x version of Scala (Linux)
- Elasticsearch 2.20 Beginners: aggregation (Server)
- Install apr support for Tomcat on Linux (Server)
- Git large file storage will help handle large binary files (Linux)
- CentOS 7.0 Enable iptables firewall (Linux)
- 11G ASM disk group does not automatically handle MOUNT (Database)
- Profile Linux users login shell and login to read (Linux)
- ORA-12154 TNS could not resolve the specified identifier (Database)
- The execution order of Oracle WHERE condition is not from right to left (Database)
- Ubuntu 14.04 Docker installation (Linux)
- Install and use automated tools Ansible in CentOS 7 (Linux)
- Linux environment password security settings (Linux)
- C ++ string in the end (Programming)
- Java semaphores (Programming)
- error 1819 (HY000): your password does not satisfy the current policy requirements (Database)
- CentOS RedHat YUM Source Extensions Supplement (including 32-bit, 64-bit) (Linux)
- Redis logging system (Database)
- C ++ overloaded stream insertion operator and the stream extraction operator (Programming)
- How to install Linux Go Language (Linux)
- Remote database using RMAN recovery test (RAC return to single-instance database) (Database)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.