python call shell command

Invoke shell command in python program

 

1. os.system(command)

This function will start the child process, execute command in the child process , and return the exit status after the command command is executed. If the command has execution content, it will be displayed on the standard output. This is actually implemented using the C standard library function system() .

    Disadvantage: This function needs to reopen a terminal when executing the command command, and cannot save the execution result of the command command.

Example: os.system('ls -l *')

2. os.popen(command,mode)

Open a pipe to the command process. The return value of this function is a file object, which can be read or written ( determined by mode , which defaults to 'r') . If mode is 'r' , you can use the return value of this function to call read() to get the execution result of the command command.

os.system(cmd) or os.popen(cmd) , the return value of the former is the exit status code of the script, and the return value of the latter is the output content during the execution of the script. In actual use, it is selected according to the needs.

Example: tmp = os.popen('ls -l *').readlines()

3. commands.getstatusoutput(command)

  Use the os.popen() function to execute the command command and return a tuple (status, output) , which respectively represent the return status and execution result of the command command execution. The execution of command is actually in the way of {command;} 2>&1 , so output contains console output information or error information. Trailing newlines are not included in output .

 

4. subprocess module

 

This module debuted in python2.4 , which concentrated many operations on the process, in which call() completely replaced system(), and popen() was replaced by the richer Popen class;

 

Summary: python provides a very complete function of calling shell commands. In actual combat, the problems I encountered can be basically solved with system and popen ;

 

Quoted from: python's method of calling shell commands  http://www.cnblogs.com/thinker-lj/p/3860123.html

 

 

Example: on the windows platform

 

 

>>> import os

>>> os.system("tasklist")

0

Phenomenon: The black shell flashes and then exits and closes, and the python output result is 0

 

 

>>> os.popen("tasklist")
<open file 'tasklist', mode 'r' at 0x02C40C28>

 

Phenomenon: output the above string

 

>>> l = os.popen("tasklist")
>>> l
<open file 'tasklist', mode 'r' at 0x02C40E38>

>>> tmp = os.popen('tasklist').readlines()
>>> tmp

Output result:

['\n', '\xd3\xb3\xcf\xf1\xc3\xfb\xb3\xc6                       PID \xbb\xe1\xbb\xb0\xc3\xfb              \xbb\xe1\xbb\xb0#       \xc4\xda\xb4\xe6\xca\xb9\xd3\xc3 \n', '========================= ======== ================ =========== ============\n', 'System Idle Process              0 Services                   0         24 K\n', 'system                           4 Services                   0        368 K\n', 'smss.exe                       328 Services                   0      1,232 K\n', 'csrss.exe                      484 Services                   0      5,748 K\n', 'wininit.exe                    744 Services                   0      5,336 K\n', 'csrss.exe                      768 Console                    1     76,116 K\n', 'services.exe                   812 Services                   0     10,964 K\n', 'winlogon.exe                   844 Console                    1     28,696 K\n', 'lsass.exe                      852 Services                   0     14,180 K\n', 'lsm.exe                        860 Services                   0      4,544 K\n', 'svchost.exe                    980 Services                   0     10,080 K\n', 'svchost.exe                    376 Services                   0      8,328 K\n', 'svchost.exe                    564 Services                   0     20,040 K\n', 'svchost.exe                    620 Services                   0    219,028 K\n', 'svchost.exe                    636 Services                   0     37,480 K\n', 'svchost.exe                   1028 Services                   0     12,580 K\n', 'igfxCUIService.exe            1080 Services                   0      7,708 K\n', 'RtkAudioService64.exe         1144 Services                   0      5,572 K\n', 'RAVBg64.exe                   1232 Console                    1     12,532 K\n', 'RAVBg64.exe                   1240 Console                    1     11,728 K\n', 'ZhuDongFangYu.exe             1264 Services                   0     45,404 K\n', 'svchost.exe                   1328 Services                   0     18,032 K\n', 'spoolsv.exe                   1668 Services                   0     15,384 K\n', 'svchost.exe                   1700 Services                   0     19,760 K\n', 'AlibabaProtect.exe            1948 Services                   0     33,248 K\n', 'AliWorkbenchSafe.exe          1984 Services                   0     25,244 K\n', 'AppleMobileDeviceService.     2008 Services                   0     11,876 K\n', 'mDNSResponder.exe             1216 Services                   0      5,872 K\n', 'sqlservr.exe                  1912 Services                   0      3,720 K\n', 'mysqld.exe                    2152 Services                   0    177,364 K\n', 'qcmtusvc.exe                  2196 Services                   0      6,448 K\n', 'QQProtect.exe                 2236 Services                   0     28,048 K\n', 'sqlwriter.exe                 2272 Services                   0      6,852 K\n', 'svchost.exe                   2292 Services                   0      5,712 K\n', 'vmnat.exe                     2344 Services                   0      5,164 K\n', 'taskhost.exe                  2680 Console                    1     12,308 K\n', 'dwm.exe                       2736 Console                    1     42,200 K\n', 'explorer.exe                  2776 Console                    1     95,128 K\n', 'WmiPrvSE.exe                  2536 Services                   0     14,444 K\n', 'vmware-authd.exe              2720 Services                   0     17,868 K\n', 'vmnetdhcp.exe                 3028 Services                   0     10,512 K\n', 'vmware-usbarbitrator64.ex     3092 Services                   0      8,112 K\n', 'RtkNGUI64.exe                 3308 Console                    1     16,420 K\n', 'TSVNCache.exe                 3468 Console                    1     12,980 K\n', 'RAVBg64.exe                   3576 Console                    1     14,392 K\n', 'RAVBg64.exe                   3584 Console                    1     12,784 K\n', '360tray.exe                   3832 Console                    1     44,948 K\n', '91AssistProxy.exe             3860 Console                    1      7,560 K\n', 'lmgrd.exe                     4040 Console                    1      6,240 K\n', 'arcd.exe                      3172 Console                    1      6,200 K\n', 'TXPlatform.exe                4808 Console                    1      2,508 K\n', 'AliworkbenchSafeUI.exe        4912 Console                    1     16,616 K\n', 'svchost.exe                   3924 Services                   0      6,276 K\n', 'svchost.exe                   4496 Services                   0     40,048 K\n', 'igfxEM.exe                    3188 Console                    1     13,488 K\n', 'igfxHK.exe                    4412 Console                    1     11,496 K\n', 'igfxTray.exe                  4508 Console                    1      9,300 K\n', 'SoftMgrLite.exe               1396 Console                    1     16,300 K\n', 'chrome.exe                    4184 Console                    1    155,048 K\n', 'chrome.exe                    5168 Console                    1      7,064 K\n', 'chrome.exe                    5480 Console                    1     10,396 K\n', 'chrome.exe                    5612 Console                    1     89,440 K\n', 'chrome.exe                    5772 Console                    1     59,028 K\n', 'chrome.exe                    5784 Console                    1     47,276 K\n', 'chrome.exe                    5796 Console                    1     27,476 K\n', 'chrome.exe                    5812 Console                    1     63,732 K\n', 'cmd.exe                       5368 Console                    1      3,768 K\n', 'conhost.exe                   5404 Console                    1      8,736 K\n', 'chromeht.exe                  2452 Console                    1      5,624 K\n', 'QQ.exe                        5844 Console                    1    386,860 K\n', 'chrome.exe                    4520 Console                    1    128,552 K\n', 'chrome.exe                    6132 Console                    1     25,400 K\n', 'QQExternal.exe                3192 Console                    1     49,604 K\n', 'QQExternal.exe                4172 Console                    1     36,160 K\n', 'wwbizsrv.exe                  6592 Services                   0      7,496 K\n', 'PanDownload.exe               6976 Console                    1     31,472 K\n', 'aria2c.exe                     688 Console                    1     11,384 K\n', 'conhost.exe                   4368 Console                    1      8,604 K\n', 'chrome.exe                    5556 Console                    1    144,956 K\n', 'wps.exe                       4232 Console                    1    213,068 K\n', 'wpscloudsvr.exe               5408 Console                    1     10,080 K\n', 'wpscenter.exe                 6896 Console                    1      9,312 K\n', 'wpscenter.exe                 6884 Console                    1      2,792 K\n', 'WmiPrvSE.exe                  6988 Services                   0     26,532 K\n', 'WmiPrvSE.exe                  7184 Services                   0     10,124 K\n', 'GeePlayer.exe                 7908 Console                    1    117,604 K\n', 'pythonw.exe                   1684 Console                    1     27,524 K\n', 'pythonw.exe                   1016 Console                    1     14,304 K\n', 'chrome.exe                    7512 Console                    1    262,116 K\n', 'cmd.exe                       7624 Console                    1      3,248 K\n', 'conhost.exe                   5232 Console                    1     15,168 K\n', 'audiodg.exe                   4356 Services                   0     21,476 K\n', 'cmd.exe                       5336 Console                    1      3,724 K\n', 'conhost.exe                   4484 Console                    1      8,696 K\n', 'tasklist.exe                  7336 Console                    1      5,996 K\n', 'cmd.exe                       5636 Console                    1      3,720 K\n', 'conhost.exe                   7044 Console                    1      8,812 K\n', 'tasklist.exe                  6524 Console                    1      5,740 K\n']
output result

Phenomenon The string text is output, but Chinese is not displayed due to the character set problem

 

take the last line

>>> s = tmp[-1]
>>> s

'tasklist.exe                  6524 Console                    1      5,740 K\n'

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324604488&siteId=291194637