Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Use Python to perform system commands common usage     - Linux Apache server security (Linux)

- Oracle 11R2 Grid Infrastructure execute root.sh script rootcrs.pl execution failed treatment (Database)

- Ubuntu users to install Xtreme Download Manager (Linux)

- Fedora 22 installation and configuration optimization (Linux)

- Memcached distributed caching (Server)

- OpenDJ installed on RHEL6 (Linux)

- Install OpenGL development environment in Fedora and Ubuntu (Linux)

- Transfer files and permissions from Windows to Linux system by Samba (Linux)

- Linux some lessons learned about network security (Linux)

- How to fix Not Enough Free Disk Space On / boot on Ubuntu (Linux)

- Vi editor Introduction (Linux)

- How open source code libraries hosted on Github (Linux)

- The development environment to build Nodejs under Ubuntu 14.04 (Linux)

- CentOS install SystemTap-2.6 (Linux)

- MySQL 5.6.12 binary log path switching binlog (Database)

- Install Redis 2.6 5.5 32 position CentOS error resolved (Linux)

- Access clipboard content across multiple vim instances in a terminal (Linux)

- Linux operating system to solve a serious fault handling (Linux)

- MySQL 5.6 Open full query log (Database)

- Let Markdown code syntax highlighting and support Django1.6 (Linux)

 
         
  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:      
 
- Linux operating system Samba server configuration and use (Server)
- Oracle SQL statement to retrieve data paging table (Database)
- C ++ Object Model Comments (Programming)
- MySQL5.7 implement virtual column expression index (Database)
- Embedded Linux to solve the problem in the time zone (Linux)
- Linux kernel source tree to establish load module hello (Linux)
- Linux system - The understanding cpu load (Linux)
- Python 3 for instructions encoded string conversion (Programming)
- Based kubernetes Construction Docker Cluster Management Comments (Server)
- Fedora 22 users to install the VLC media player (Linux)
- How to install the client sqlplus under linux (Database)
- Three methods easy data encryption on Linux (Linux)
- Use ISO document production OpenStack used CoreOS mirror (Linux)
- Dockerfile use to build a mirror-based CentOS 7 (Linux)
- Single-node Hadoop installation notes distributed pseudo & (Server)
- Linux install Eclipse for C / C ++ Development (Linux)
- Linux environment Duplex (multi-machine) automatic mutual backup scheme (Linux)
- DRBD Principle and Features Overview (Server)
- Upgrade to Node V4 seven reasons (Programming)
- Tomcat configuration memory and remote debug port (Server)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.