>>> import os
>>> os.system('ls')
blog.tar djangoblog.log managerblog.sh test.txt
data ENV nginx-1.15.11 wget-log
0
提示:python3已結(jié)廢棄
例子:
>>> import os
>>> os.popen('ls')
>>> os.popen('ls').read()
'blog.tar\ndata\ndb.sql\ndead.letter\nDjangoBlog\ndjangoblog.log\nENV\nfile1\nget-pip.py\nindex.html\nmanagerblog.sh\nnginx-1.15.11\nPython-3.5.2\nstartDjangoBlog.sh\nstart_uwsgi.ini\ntest.txt\nwget-log\nzrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war\n'
>>> os.popen('ls').read().split('\n')
['blog.tar', 'data', 'db.sql', 'dead.letter', 'DjangoBlog', 'djangoblog.log', 'ENV', 'file1', 'get-pip.py', 'index.html', 'managerblog.sh', 'nginx-1.15.11', 'Python-3.5.2', 'startDjangoBlog.sh', 'start_uwsgi.ini', 'test.txt', 'wget-log', 'zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war', '']
獲取命令執(zhí)行的結(jié)果,但是沒有命令的執(zhí)行狀態(tài),這樣可以將獲取的結(jié)果保存起來放到list中。
>>> import subprocess
>>> status,result=subprocess.getstatusoutput('ls')
>>> status
0
>>> result
'blog.tar\ndata\ndb.sql\ndead.letter\nDjangoBlog\ndjangoblog.log\nENV\nfile1\nget-pip.py\nindex.html\nmanagerblog.sh\nnginx-1.15.11\nPython-3.5.2\nstartDjangoBlog.sh\nstart_uwsgi.ini\ntest.txt\nwget-log\nzrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war'
>>> result.split('\n')
['blog.tar', 'data', 'db.sql', 'dead.letter', 'DjangoBlog', 'djangoblog.log', 'ENV', 'file1', 'get-pip.py', 'index.html', 'managerblog.sh', 'nginx-1.15.11', 'Python-3.5.2', 'startDjangoBlog.sh', 'start_uwsgi.ini', 'test.txt', 'wget-log', 'zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war']
2、getoutput
>>> print(subprocess.getoutput('ls').split('\n') )
['blog.tar', 'data', 'db.sql', 'dead.letter', 'DjangoBlog', 'djangoblog.log', 'ENV', 'file1', 'get-pip.py', 'index.html', 'managerblog.sh', 'nginx-1.15.11', 'Python-3.5.2', 'startDjangoBlog.sh', 'start_uwsgi.ini', 'test.txt', 'wget-log', 'zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war']
3、call
執(zhí)行命令,返回狀態(tài)碼(命令正常執(zhí)行返回0,報(bào)錯(cuò)則返回1)
>>> subprocess.call('ls')
blog.tar djangoblog.log managerblog.sh test.txt
data ENV nginx-1.15.11 wget-log
db.sql file1 Python-3.5.2 zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war
dead.letter get-pip.py startDjangoBlog.sh
DjangoBlog index.html start_uwsgi.ini
0
4、check_call
執(zhí)行命令,如果執(zhí)行成功則返回狀態(tài)碼0,否則拋異常
>>> subprocess.check_call('ls')
blog.tar djangoblog.log managerblog.sh test.txt
data ENV nginx-1.15.11 wget-log
db.sql file1 Python-3.5.2 zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war
dead.letter get-pip.py startDjangoBlog.sh
DjangoBlog index.html start_uwsgi.ini
0
>>> subprocess.check_call('ls luojun')
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/python3/lib/python3.5/subprocess.py", line 576, in check_call
retcode = call(*popenargs, **kwargs)
File "/usr/local/python3/lib/python3.5/subprocess.py", line 557, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/local/python3/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/local/python3/lib/python3.5/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'ls luojun'
5、check_output
執(zhí)行命令,如果執(zhí)行成功則返回執(zhí)行結(jié)果,否則拋異常
>>> subprocess.check_output('ls')
b'blog.tar\ndata\ndb.sql\ndead.letter\nDjangoBlog\ndjangoblog.log\nENV\nfile1\nget-pip.py\nindex.html\nmanagerblog.sh\nnginx-1.15.11\nPython-3.5.2\nstartDjangoBlog.sh\nstart_uwsgi.ini\ntest.txt\nwget-log\nzrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war\n'
>>> subprocess.check_output('ls luojun')
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/python3/lib/python3.5/subprocess.py", line 626, in check_output
**kwargs).stdout
File "/usr/local/python3/lib/python3.5/subprocess.py", line 693, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/local/python3/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/local/python3/lib/python3.5/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'ls luojun'
6、subprocess.Popen(…)
用于執(zhí)行復(fù)雜的系統(tǒng)命令
參數(shù) 注釋
args shell命令,可以是字符串或者序列類型(如:list,元組)
bufsize 指定緩沖。0 無緩沖,1 行緩沖,其他 緩沖區(qū)大小,負(fù)值 系統(tǒng)緩沖
stdin, stdout, stderr 分別表示程序的標(biāo)準(zhǔn)輸入、輸出、錯(cuò)誤句柄
preexec_fn 只在Unix平臺(tái)下有效,用于指定一個(gè)可執(zhí)行對(duì)象(callable object),它將在子進(jìn)程運(yùn)行之前被調(diào)用
close_sfs 在windows平臺(tái)下,如果close_fds被設(shè)置為True,則新創(chuàng)建的子進(jìn)程將不會(huì)繼承父進(jìn)程的輸入、輸出、錯(cuò)誤管道。所以不能將close_fds設(shè)置為True同時(shí)重定向子進(jìn)程的標(biāo)準(zhǔn)輸入、輸出與錯(cuò)誤(stdin, stdout, stderr)。
shell 同上
cwd 用于設(shè)置子進(jìn)程的當(dāng)前目錄
env 用于指定子進(jìn)程的環(huán)境變量。如果env = None,子進(jìn)程的環(huán)境變量將從父進(jìn)程中繼承。
universal_newlines 不同系統(tǒng)的換行符不同,True -> 同意使用 \n
startupinfo 只在windows下有效,將被傳遞給底層的CreateProcess()函數(shù),用于設(shè)置子進(jìn)程的一些屬性,如:主窗口的外觀,進(jìn)程的優(yōu)先級(jí)等等
createionflags 同上
[root@VM_0_2_centos test]# vim test.sh
[root@VM_0_2_centos test]# cat test.sh
#!/bin/bash
echo "this is my test shell ${1} ${2}"
exit 0
[root@VM_0_2_centos test]# chmod +x test.sh
[root@VM_0_2_centos test]# /test/test.sh jluo jluocc.cn
this is my test shell jluo jluocc.cn
[root@VM_0_2_centos test]# vim mytest.py
[root@VM_0_2_centos test]# cat mytest.py
#! /usr/bin/env python3
import os
import sys
if(len(sys.argv)<3):
print("please input two arguments")
sys.exit(1)
arg0 = sys.argv[1]
arg1 = sys.argv[2]
print('=====file name *.py全路徑=====')
print(sys.argv[0])
print('=====腳本執(zhí)行結(jié)果如下=====')
os.system('/test/test.sh '+arg0+' '+arg1)
[root@VM_0_2_centos test]# python3 mytest.py jluo jluocc.cn
=====file name *.py全路徑=====
mytest.py
=====腳本執(zhí)行結(jié)果如下=====
this is my test shell jluo jluocc.cn
[root@VM_0_2_centos test]# python3 /test/mytest.py jluo jluocc.cn
=====file name *.py全路徑=====
/test/mytest.py
=====腳本執(zhí)行結(jié)果如下=====
this is my test shell jluo jluocc.cn
結(jié)束語:
更多精彩內(nèi)容持續(xù)更新中,關(guān)注微信公眾號(hào),有你更精彩。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。