>>> import os
>>> os.system('ls')
blog.tar djangoblog.log managerblog.sh test.txt
data ENV nginx-1.15.11 wget-log
0
提示:python3已結廢棄
例子:
創(chuàng)新互聯(lián)是一家專業(yè)提供那曲企業(yè)網(wǎng)站建設,專注與成都網(wǎng)站制作、成都網(wǎng)站設計、外貿(mào)營銷網(wǎng)站建設、HTML5建站、小程序制作等業(yè)務。10年已為那曲眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡公司優(yōu)惠進行中。
>>> import os
>>> os.popen('ls')
<os._wrap_close object at 0x7f0acd548b00>
>>> 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í)行的結果,但是沒有命令的執(zhí)行狀態(tài),這樣可以將獲取的結果保存起來放到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,報錯則返回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
04、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 "<stdin>", line 1, in <module>
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í)行結果,否則拋異常
>>> 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 "<stdin>", line 1, in <module>
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í)行復雜的系統(tǒng)命令
參數(shù) 注釋
args shell命令,可以是字符串或者序列類型(如:list,元組)
bufsize 指定緩沖。0 無緩沖,1 行緩沖,其他 緩沖區(qū)大小,負值 系統(tǒng)緩沖
stdin, stdout, stderr 分別表示程序的標準輸入、輸出、錯誤句柄
preexec_fn 只在Unix平臺下有效,用于指定一個可執(zhí)行對象(callable object),它將在子進程運行之前被調(diào)用
close_sfs 在windows平臺下,如果close_fds被設置為True,則新創(chuàng)建的子進程將不會繼承父進程的輸入、輸出、錯誤管道。所以不能將close_fds設置為True同時重定向子進程的標準輸入、輸出與錯誤(stdin, stdout, stderr)。
shell 同上
cwd 用于設置子進程的當前目錄
env 用于指定子進程的環(huán)境變量。如果env = None,子進程的環(huán)境變量將從父進程中繼承。
universal_newlines 不同系統(tǒng)的換行符不同,True -> 同意使用 \n
startupinfo 只在windows下有效,將被傳遞給底層的CreateProcess()函數(shù),用于設置子進程的一些屬性,如:主窗口的外觀,進程的優(yōu)先級等等
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í)行結果如下=====')
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í)行結果如下=====
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í)行結果如下=====
this is my test shell jluo jluocc.cn結束語:
更多精彩內(nèi)容持續(xù)更新中,關注微信公眾號,有你更精彩。.jpg)
分享名稱:python和shell的結合與應用
URL網(wǎng)址:http://chinadenli.net/article42/ppdsec.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設計公司、外貿(mào)網(wǎng)站建設、網(wǎng)站策劃、關鍵詞優(yōu)化、靜態(tài)網(wǎng)站、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)