模塊簡介javascript
1、time模塊
2、sys模塊
3、datetime模塊
4、pickle模塊
5、json模塊
6、OS模塊
7、hashlib加密模塊
8、第三方模塊的安裝方法
9、requests模塊
10、XML模塊
11、configparser模塊
12、shutil
十3、subprocess模塊
十4、logging模塊php
模塊的分類html
一、內置模塊 java
二、自定義模塊node
三、第三方模塊(須要安裝)python
模塊前言:模塊的導入:web
模塊的導入有兩點須要注意:算法
一、在sys.path中的能夠直接進行導入,由於sys.path返回的是列表,因此能夠利用append把路徑加入到列表當中shell
二、把模塊導入後,再次導入模塊,python會檢測內存中是否存在該模塊,若是存在該模塊則會忽略導入django
1、time模塊
一、time.sleep(5) #等待5秒鐘
#!usr/bin/env python # -*- coding:utf-8 -*- import time print('start to sleep.....') time.sleep(5) #等待5秒 print('wake up.....')
#!usr/bin/env python # -*- coding:utf-8 -*- import time print(time.clock()) #返回處理器時間,3.3已經廢棄 print(time.process_time()) #返回處理器時間,3.3已經廢棄 print(time.time()) #返回時間戳,1970年1月1號0:00至如今一共多少秒,全部系統的時間戳都是從該時間計時的 print(time.ctime(time.time())) #將時間戳轉化爲字符串,Thu Feb 16 10:10:20 2017 print(time.ctime(time.time()-24*60*60)) #將時間轉化爲字符串,昨天的這個時間,Thu Feb 15 10:10:20 2017 print(time.gmtime(time.time())) #返回time.struct_time對象,能夠分別提取年月日時分秒 print(time.localtime(time.time())) #返回time.struct_time對象,對象顯示的是本地時間,能夠分別提取年月日時分秒 print(time.mktime(time.localtime(time.time()))) #與localtime功能相反,將time.struct_time對象轉化爲時間戳 print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) #將time.struct_time對象轉化爲字符串格式
print(time.strftime('%Y-%m-%d %H:%M:%S') #輸出當前時間
print(time.strptime('2017-2-16 11:40:27','%Y-%m-%d %H:%M:%S')) #將字符串轉化爲time.struct_time對象
#!usr/bin/env python # -*- coding:utf-8 -*- #time.gmtime()的用法 #time.localtime()的用法 import time time_object = time.gmtime(time.time()) print(time_object) #time.struct_time(tm_year=2017, tm_mon=2, tm_mday=16, # tm_hour=2, tm_min=35, tm_sec=54, tm_wday=3, tm_yday=47, tm_isdst=0) time_str = '%s-%s-%s %s:%s:%s' %(time_object.tm_year,time_object.tm_mon,time_object.tm_mday,time_object.tm_hour,time_object.tm_min,time_object.tm_sec) print(time_str) #2017-2-16 2:45:26 格林威治時間
2、sys模塊
1)sys.argv ----命令參數List,第一個參數是程序自己路徑
import sys print(sys.argv) if sys.argv[1] == 'one': print('11111') else: print('.....')
在終端中運行結果以下:
E:\python_code\2\11day>python time2.py one
['time2.py', 'one']
11111
2)sys.path ----返回模塊的搜索路徑,初始化時使用PYTHONPATH環境變量的值
#!usr/bin/env python # -*- coding:utf-8 -*- import sys print(sys.path) #打印輸出: #['E:\\python_code\\2\\11day', # 'E:\\python_code\\2', # 'D:\\python_lib', # 'D:\\Program Files (x86)\\Python36\\python36.zip', # 'D:\\Program Files (x86)\\Python36\\DLLs', # 'D:\\Program Files (x86)\\Python36\\lib', # 'D:\\Program Files (x86)\\Python36', # 'D:\\Program Files (x86)\\Python36\\lib\\site-packages']
當導入python包時,會從sys.path路徑返回的列表中查找包,找到則運行,並返回。
通常第三方安裝的路徑都是在:
D:\\Program Files (x86)\\Python36\\lib\\site-packages
3)sys.exit(n) ----退出程序,正常退出時exit(0)
#!usr/bin/env python # -*- coding:utf-8 -*- import sys st = input('退出程序:q;輸入1打印one;輸入2打印two:') if st == 'q': sys.exit('goodbye!') #也能夠用:exit('goodbye!'),退出當前程序 elif st == '1': print('one') elif st == '2': print('two') print('thanks!')
4)sys.version ----獲取python解釋器程序的版本信息
#!usr/bin/env python # -*- coding:utf-8 -*- import sys print(sys.version) #打印輸出: #3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)]
5)sys.platform ----返回操做系統平臺名稱
#!usr/bin/env python # -*- coding:utf-8 -*- import sys print(sys.platform) #打印輸出win32
6)sys.stdout.wirte("#") #在同一行中輸出字符串,能夠作進度條原理
pip.exe install django ----從網絡下載django並安裝,顯示安裝百分比和進度條
pip uninstall django ----卸載django
import sys import time for i in range(20): sys.stdout.write('#') time.sleep(0.3)
在終端中輸入上面的代碼,能夠像進度條同樣不斷輸出#
7)sys.stdin.readline() #屏幕輸入函數
#!usr/bin/env python # -*- coding:utf-8 -*- import sys val = sys.stdin.readline()[:-1] #[:-1]能夠用切片截取輸入的字符串 print(val) #將輸入的內容打印出來,input函數底層實際也調用的這個函數
8) 練習:
讀取用戶輸入的目錄,根據用戶的輸入,建立一個相應的目錄
# -*- coding:utf-8 -*- import sys,os os.mkdir(sys.argv[1])
本身寫一個簡單的腳本,能夠在任何路徑導入
本身作一個帶百分比的進度條
#!usr/bin/env python # -*- coding:utf-8 -*- import sys,time for i in range(11): sys.stdout.write('\r') #回車符 sys.stdout.write('%s%% %s' %(i *10,'#' * i)) sys.stdout.flush() #從顯存刷新到屏幕,沒打印一次刷新一次屏幕 time.sleep(0.2)
3、datetime模塊
#時間加減 import datetime print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925 print(datetime.date.fromtimestamp(time.time()) ) # 時間戳直接轉成日期格式 2016-08-19 print(datetime.datetime.now() ) print(datetime.datetime.now() + datetime.timedelta(3)) #當前時間+3天 print(datetime.datetime.now() + datetime.timedelta(-3)) #當前時間-3天 print(datetime.datetime.now() + datetime.timedelta(hours=3)) #當前時間+3小時 print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #當前時間+30分,
參數:days=0, seconds=0, microseconds=0,
milliseconds=0, minutes=0, hours=0, weeks=0
c_time = datetime.datetime.now() print(c_time.replace(minute=3,hour=2)) #時間替換
4、pickle模塊
一、把字典轉化爲字符串寫入文件
#!usr/bin/env python # -*- coding:utf-8 -*- accounts = {1000: {'name':'sunshuhai', 'email':'888@163.com', 'password':'888888', 'balance':15000, 'phone':'13863366666', 'bank_num':{'ICBC':'6220216160004852','CBC':'64545455'} }, 1001: {'name': 'zhangsan', 'email': '777@163.com', 'password': '777777', 'balance': 12000, 'phone': '13863377777', 'bank_num': {'ICBC': '62224855', 'CBC': '88045454'} }, } f = open('ac.txt','w',encoding='utf-8') text = str(accounts) print(text) f.write(text) f.close()
但寫入文件後,進行沒法進行修改,因此能夠用pickle模塊
二、pickle把字典轉爲字節類型存入文件
#!usr/bin/env python # -*- coding:utf-8 -*- accounts = {1000: {'name':'sunshuhai', 'email':'888@163.com', 'password':'888888', 'balance':15000, 'phone':'13863366666', 'bank_num':{'ICBC':'6220216160004852','CBC':'64545455'} }, 1001: {'name': 'zhangsan', 'email': '777@163.com', 'password': '777777', 'balance': 12000, 'phone': '13863377777', 'bank_num': {'ICBC': '62224855', 'CBC': '88045454'} }, } import pickle f = open('ac.txt','wb') text = pickle.dumps(accounts) #返回的是(bytes)字節類型,將字典類型轉化爲字節類型,無論以何種方式打開文件,均返回字節型,因此必須用wb方式打開能夠直接寫入 f.write(text) #寫入文件 f.close()
dumps與loads均以字節的形式寫入和讀取的
#!usr/bin/env python # -*- coding:utf-8 -*- import pickle f = open('ac.txt','rb') f_read = f.read() #把pickle寫入的字符串還原爲字節形式 print(f_read) dic_text = pickle.loads(f_read) #pickle.dumps存儲的字符串轉化爲字典 print(type(dic_text)) #打印輸出:<class 'dict'>
例子:修改文件中ID爲1000的balance的值+500
#!usr/bin/env python # -*- coding:utf-8 -*- import pickle f = open('ac.txt','rb') text = f.read() #讀取文本內容 dic_text = pickle.loads(text) #利用pickle將文本內容轉化爲字典類型 dic_text[1000]['balance'] += 500 #修改字典中的值 f.close() f = open('ac.txt','wb') text_write = pickle.dumps(dic_text) #將字典轉化爲文本內容 f.write(text_write) #把文本內容從新覆蓋寫入文件 f.close() f = open('ac.txt','rb') text_read = f.read() #讀取文件中的文本內容 text_dic = pickle.loads(text_read) #利用pickle將文本內容轉化爲字典 print(type(text_dic),text_dic) #打印字典的類型和字典內容
三、pickle中的dump和load 與 dumps和loads的區別
dumps能夠將字典轉爲字節,而後以字節方式打開寫入文件
dump 能夠直接將字典以字節方式打開的文件
loads 能夠將字節類型的字符串,直接轉化爲字典
load能夠將以字節方式打開的文件對象直接轉化爲字典
#!usr/bin/env python # -*- coding:utf-8 -*- accounts = {1000: {'name':'sunshuhai', 'email':'888@163.com', 'password':'888888', 'balance':15000, 'phone':'13863366666', 'bank_num':{'ICBC':'6220216160004852','CBC':'64545455'} }, 1001: {'name': 'zhangsan', 'email': '777@163.com', 'password': '777777', 'balance': 12000, 'phone': '13863377777', 'bank_num': {'ICBC': '62224855', 'CBC': '88045454'} }, } import pickle f = open('ac.txt','wb') pickle.dump(accounts,f) #dump能夠直接把字典寫入文件 f.close() f = open('ac.txt','rb') dic = pickle.load(f) #load能夠直接把文件內容讀出爲字典類型 print(type(dic),dic)
5、json模塊
json模塊與pickle模塊用法一致;
區別:
一、pickle模塊已字節的方式進行轉化,而json是以字符串的方式進行轉化。
二、pickle模塊能夠將任何數據類型序列化,但json不支持某些數據類型的序列化,如data,json只支持字典、列表、簡單的變量、字符串等,json不支持元組,稍微複雜的數據類型就不能處理了
三、pickle只在python語言支持,不一樣語言之間、不一樣系統之間的接口、數據交互爲了通用性,通常選擇json。
dumps和loads的用法:
#!usr/bin/env python # -*- coding:utf-8 -*- accounts = {1000: {'name':'sunshuhai', 'email':'888@163.com', 'password':'888888', 'balance':15000, 'phone':'13863366666', 'bank_num':{'ICBC':'6220216160004852','CBC':'64545455'} }, 1001: {'name': 'zhangsan', 'email': '777@163.com', 'password': '777777', 'balance': 12000, 'phone': '13863377777', 'bank_num': {'ICBC': '62224855', 'CBC': '88045454'} }, } import json f = open('ac.txt','w',encoding='utf-8') text = json.dumps(accounts) #將字典轉化爲字符串 f.write(text) #將轉化後的字符串寫入文件 f.close() f = open('ac.txt','r',encoding='utf-8') text1 = f.read() #讀取文件中的字符串 dic = json.loads(text1) #將字符串轉化爲字典 print(type(dic),dic) f.close()
dump和load的用法:
#!usr/bin/env python # -*- coding:utf-8 -*- accounts = {1000: {'name':'sunshuhai', 'email':'888@163.com', 'password':'888888', 'balance':15000, 'phone':'13863366666', 'bank_num':{'ICBC':'6220216160004852','CBC':'64545455'} }, 1001: {'name': 'zhangsan', 'email': '777@163.com', 'password': '777777', 'balance': 12000, 'phone': '13863377777', 'bank_num': {'ICBC': '62224855', 'CBC': '88045454'} }, } import json #dump和load f = open('ac.txt','w',encoding='utf-8') json.dump(accounts,f) #直接將accounts轉化爲文本,寫入f對象 f.close() f = open('ac.txt','r',encoding='utf-8') dic = json.load(f) #直接讀取f對象中的文件,並轉化爲字典 print(type(dic),dic)
注意:python代碼中列表、字典等內部儘可能的字符串使用雙號,若是將含有單引號的字符串傳給json,則會報錯,因此之後列表、字典等內部的字符串儘可能使用雙引號loads時使用雙引號表示字符串,dumps可使用單引號表示字符串,例如:
#!usr/bin/env python # -*- coding:utf-8 -*- import json st = '{"name":"sunshuhai","age":100}' #若是st = "{'name':'sunshuhai','age':100}"則會報錯 st_dic = json.loads(st) print(type(st_dic),st_dic)
json不支持元組
tup = (11,22,33) import json r2 = json.dumps(tup) print(r2) #打印輸出[11, 22, 33] s = '("11","22","33")' import json r1 = json.loads(s) print(r1) #會報錯,由於json不支持元組,只支持列表和字典
6、OS模塊
1 os.getcwd() 獲取當前工做目錄,即當前python腳本工做的目錄路徑 2 os.chdir("dirname") 改變當前腳本工做目錄;至關於shell下cd 3 os.curdir 返回當前目錄: ('.') 4 os.pardir 獲取當前目錄的父目錄字符串名:('..') 5 os.makedirs('dir1/dir2') 可生成多層遞歸目錄 6 os.removedirs('dirname1') 若目錄爲空,則刪除,並遞歸到上一級目錄,如若也爲空,則刪除,依此類推 7 os.mkdir('dirname') 生成單級目錄;至關於shell中mkdir dirname 8 os.rmdir('dirname') 刪除單級空目錄,若目錄不爲空則沒法刪除,報錯;至關於shell中rmdir dirname 9 os.listdir('dirname') 列出指定目錄下的全部文件和子目錄,包括隱藏文件,並以列表方式打印 10 os.remove() 刪除一個文件 11 os.rename("oldname","new") 重命名文件/目錄 12 os.stat('path/filename') 獲取文件/目錄信息 13 os.sep 操做系統特定的路徑分隔符,win下爲"\\",Linux下爲"/" 14 os.linesep 當前平臺使用的行終止符,win下爲"\t\n",Linux下爲"\n" 15 os.pathsep 用於分割文件路徑的字符串 16 os.name 字符串指示當前使用平臺。win->'nt'; Linux->'posix' 17 os.system("bash command") 運行shell命令,直接顯示 18 os.environ 獲取系統環境變量 19 os.path.abspath(path) 返回path規範化的絕對路徑 20 os.path.split(path) 將path分割成目錄和文件名二元組返回 21 os.path.dirname(path) 返回path的目錄。其實就是os.path.split(path)的第一個元素 22 os.path.basename(path) 返回path最後的文件名。如何path以/或\結尾,那麼就會返回空值。即os.path.split(path)的第二個元素 23 os.path.exists(path) 若是path存在,返回True;若是path不存在,返回False 24 os.path.isabs(path) 若是path是絕對路徑,返回True 25 os.path.isfile(path) 若是path是一個存在的文件,返回True。不然返回False 26 os.path.isdir(path) 若是path是一個存在的目錄,則返回True。不然返回False 27 os.path.join(path1[, path2[, ...]]) 將多個路徑組合後返回,第一個絕對路徑以前的參數將被忽略 28 os.path.getatime(path) 返回path所指向的文件或者目錄的最後存取時間 29 os.path.getmtime(path) 返回path所指向的文件或者目錄的最後修改時間
7、hashlib加密模塊
用於加密相關的操做,代替了md5模塊和sha模塊,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
import hashlib # ######## md5 ######## hash = hashlib.md5() # help(hash.update) hash.update(bytes('admin', encoding='utf-8')) print(hash.hexdigest()) print(hash.digest()) ######## sha1 ######## hash = hashlib.sha1() hash.update(bytes('admin', encoding='utf-8')) print(hash.hexdigest()) # ######## sha256 ######## hash = hashlib.sha256() hash.update(bytes('admin', encoding='utf-8')) print(hash.hexdigest()) # ######## sha384 ######## hash = hashlib.sha384() hash.update(bytes('admin', encoding='utf-8')) print(hash.hexdigest()) # ######## sha512 ######## hash = hashlib.sha512() hash.update(bytes('admin', encoding='utf-8')) print(hash.hexdigest()) 以上加密算法雖然依然很是厲害,但時候存在缺陷,即:經過撞庫能夠反解。因此,有必要對加密算法中添加自定義key再來作加密。 import hashlib # ######## md5 ######## hash = hashlib.md5(bytes('898oaFs09f',encoding="utf-8")) #加鹽 hash.update(bytes('admin',encoding="utf-8")) print(hash.hexdigest()) #python內置還有一個 hmac(哈希信息驗證碼 Hash Message Authentication Code) 模塊,它內部對咱們建立 key 和 內容 進行進一步的處理而後再加密 import hmac h = hmac.new(bytes('898oaFs09f',encoding="utf-8")) h.update(bytes('admin',encoding="utf-8")) print(h.hexdigest())
實例:加密帳號註冊及登陸
#!usr/bin/env python # -*- coding:utf-8 -*- import hashlib def register(user,pwd): with open('user_information','a',encoding='utf-8') as f: f.write(user + '|' + md5(pwd) + '\r') def login(user,pwd): with open('user_information','r',encoding='utf-8') as f: for line in f: u,p = line.strip().split('|') if user == u and md5(pwd) == p: return True return False def md5(arg): hash = hashlib.md5(bytes('aaatttbbbccceee',encoding='utf-8')) hash.update(bytes(arg,encoding='utf-8')) return hash.hexdigest() option = input('用戶註冊請按1,用戶登錄請按2:') if option == '1': user = input('請輸入用戶名:') pwd = input('請輸入密碼:') register(user,pwd) elif option == '2': user = input('請輸入用戶名:') pwd = input('請輸入密碼:') if login(user,pwd): print('登錄成功!') else: print('登錄失敗!')
8、第三方模塊的安裝方法
第三方模塊的安裝有兩種方式:
1)軟件工具安裝,例如要安裝requests
通常python3中默認都安裝着pip3,路徑爲:D:\Program Files (x86)\Python36\Scripts
2)源碼安裝
9、requests模塊
一、requests獲取HTML
#!usr/bin/env python # -*- coding:utf-8 -*- import requests response = requests.get('http://www.baidu.com') response.encoding = 'utf-8' html = response.text print(html)
二、利用requests模塊和xml模塊判斷QQ是否在線
#!usr/bin/env python # -*- coding:utf-8 -*- import requests response = requests.get('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=258888289') response.encoding = 'utf-8' result = response.text print(result) # <?xml version="1.0" encoding="utf-8"?> # <string xmlns="http://WebXml.com.cn/">Y</string> #須要拿到上面XML中的Y from xml.etree import ElementTree as ET xml_obj = ET.XML(result) if xml_obj.text == 'Y': print('您要查找的QQ在線!') else: print('您要查找的QQ離線!')
三、利用requests 模塊查找列車車次信息
#!usr/bin/env python # -*- coding:utf-8 -*- import requests response = requests.get('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=G666&UserID=') response.encoding = 'utf-8' result = response.text from xml.etree import ElementTree as ET root = ET.XML(result) for node in root.iter('TrainDetailInfo'): #print(node.tag,node.attrib) #tag是表示標籤名,attrib表示屬性 print(node.find('TrainStation').text,node.find('ArriveTime').text,node.find('StartTime').text,node.find('KM').text)
#!/usr/bin/env python # -*- coding:utf-8 -*- import requests import json import xlwings as xw def PostDataA(): wb = xw.Book.caller() mastUrl = wb.sheets["配置表"].range('B1').value #主地址 #wb.sheets["Sheet99"].range('D8').value = "" coo = wb.sheets["Sheet99"].range('D1').value #coo = '{"_c_cookie": "1", "_c_aut": "nm%3FwuskI%60p%2CX%5Ezo3FrAF%60%2CQTNEi_PTp_%2CnN%40By_oSP%3D%2CtnR%5EbpWg%7C%7E","PHPSESSID": "le50ak8oncg4cvv0kr5dum2d17", "g_language": "zh-cn", "_c_chk": "7814UHBHV18GT0FEUw%3D%3D"}' cookie_dic = json.loads(coo) #原cookie字典 heads = { "Accept":"application/json, text/javascript, */*; q=0.01", "Accept-Encoding":"gzip, deflate, sdch", "Accept-Language":"zh-CN,zh;q=0.9", "Connection":"keep-alive", "Host": "ww2671.ws5170.com", "Referer": mastUrl + "op.php?op=member_5h&fp=bet_beforedo&palygroup=r1&gametype=24", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36", "X-Requested-With":"XMLHttpRequest", } data_str = wb.sheets["Sheet99"].range('D8').value data_dic = json.loads(data_str) r = requests.post(mastUrl + 'op.php?op=member_5h&fp=bet_do&palygroup=r1&gametype=24&disk=4',cookies=cookie_dic,headers=heads,data=data_dic) # r.encoding='gb2312' html=r.text if html[0:13]=="<script>paren": wb.sheets["Sheet99"].range('D7').value="Y"
10、XML模塊
單行XML解析
response = '<string xmlns="http://WebXml.com.cn/">Y</string>' from xml.etree import ElementTree as ET node = ET.XML(response) print(node.text) #打印輸出 Y
創建XML文件代碼爲例,文件名爲first.xml:
<data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year>2023</year> <gdppc>141100</gdppc> <neighbor direction="E" name="Austria" /> <neighbor direction="W" name="Switzerland" /> </country> <country name="Singapore"> <rank updated="yes">5</rank> <year>2026</year> <gdppc>59900</gdppc> <neighbor direction="N" name="Malaysia" /> </country> <country name="Panama"> <rank updated="yes">69</rank> <year>2026</year> <gdppc>13600</gdppc> <neighbor direction="W" name="Costa Rica" /> <neighbor direction="E" name="Colombia" /> </country> </data>
讀取xml文件爲字符串,將字符串轉化爲XML對象:
#!usr/bin/env python # -*- coding:utf-8 -*- with open('first.xml','r',encoding='utf-8') as f: xml_text = f.read() from xml.etree import ElementTree as ET root = ET.XML(xml_text) #這是<data>層 for node in root: #循環的是data下層,也就是country層 print(node.find('rank').tag) #打印標籤 print(node.find('rank').attrib) #打印rank標籤的屬性 print(node.find('rank').text) #打印rank標籤中的內容
from xml.etree import ElementTree as ET root = ET.XML(st) for r in root.iter('country'): #表示跟節點下面的全部子孫標籤中country的節點集合 print(r.tag) # country # country # country
#遍歷全部標籤的內容 from xml.etree import ElementTree as ET root = ET.XML(st) for r2 in root: #root表示country層,那麼r2表示data層下的每個country層 for r3 in r2: #r2是country層,那麼r3就是country下面的每一個標籤層 print(r3.text) #打印r3層的每一個標籤的內容
直接將xml文件轉化爲xml對象,此方式能夠修改xml對象並更新文件:
#!usr/bin/env python # -*- coding:utf-8 -*- from xml.etree import ElementTree as ET tree = ET.parse('first.xml') #將文件讀入內存並解析爲xml對象 root = tree.getroot() #獲取對象中的第一層根目錄 print(root.tag) #打印輸出data for node in root.iter('year'): #root下面的year標籤進行循環 new_year = int(node.text) + 1 #將year標籤中的內容轉爲int類型而且加1 node.text = str(new_year) #將new_year轉化爲字符串從新複製給year標籤中的內容 node.set('name','sea') #給year標籤增長一個屬性:name='sea' node.set('age','28') #給year標籤增長一個屬性:age='28' del node.attrib['name'] #把year標籤中的name屬性刪除 tree.write('first.xml') #將內存中修改完畢的xml字符串,從新寫進first.xml
刪除節點:
#!usr/bin/env python # -*- coding:utf-8 -*- #刪除節點 from xml.etree import ElementTree as ET tree = ET.parse('first.xml') print(tree) root = tree.getroot() for country in root.findall('country'): rank = int(country.find('rank').text) if rank > 50: root.remove(country) #刪除root根下的country節點 tree.write('first.xml')
打印xml下root的主要功能:
#!usr/bin/env python # -*- coding:utf-8 -*- #打印功能 from xml.etree import ElementTree as ET tree = ET.parse('first.xml') root = tree.getroot() print(dir(root)) #root功能有: ['__class__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'attrib', 'clear', 'extend', 'find', 'findall', 'findtext', 'get', 'getchildren', 'getiterator', 'insert', 'items', 'iter', 'iterfind', 'itertext', 'keys', 'makeelement', 'remove', 'set', 'tag', 'tail', 'text']
經常使用的功能:
tag attrib find set get iter
建立XML文檔實例:
#!usr/bin/env python # -*- coding:utf-8 -*- from xml.etree import ElementTree as ET #建立跟節點 new_xml = ET.Element('namelist') #建立子節點 name1 = ET.SubElement(new_xml,'name',attrib={"enrolled":"yes"}) age1 = ET.SubElement(name1,'age',attrib={"checked":"no"}) sex1 = ET.SubElement(name1,'sex') sex1.text = '33' #建立根節點的子節點 name2 = ET.SubElement(new_xml,'name',attrib={"enrolled":"no"}) age2 = ET.SubElement(name2,'age') age2.text = '19' #寫入硬盤 et = ET.ElementTree(new_xml) #建立ElementTree類的對象,參數是根節點,該對象有write方法 et.write('test.xml',encoding='utf-8',xml_declaration=True)
操做XML
XML格式類型是節點嵌套節點,對於每個節點均有如下功能,以便對當前節點進行操做:
1 class Element: 2 """An XML element. 3 4 This class is the reference implementation of the Element interface. 5 6 An element's length is its number of subelements. That means if you 7 want to check if an element is truly empty, you should check BOTH 8 its length AND its text attribute. 9 10 The element tag, attribute names, and attribute values can be either 11 bytes or strings. 12 13 *tag* is the element name. *attrib* is an optional dictionary containing 14 element attributes. *extra* are additional element attributes given as 15 keyword arguments. 16 17 Example form: 18 <tag attrib>text<child/>...</tag>tail 19 20 """ 21 22 當前節點的標籤名 23 tag = None 24 """The element's name.""" 25 26 當前節點的屬性 27 28 attrib = None 29 """Dictionary of the element's attributes.""" 30 31 當前節點的內容 32 text = None 33 """ 34 Text before first subelement. This is either a string or the value None. 35 Note that if there is no text, this attribute may be either 36 None or the empty string, depending on the parser. 37 38 """ 39 40 tail = None 41 """ 42 Text after this element's end tag, but before the next sibling element's 43 start tag. This is either a string or the value None. Note that if there 44 was no text, this attribute may be either None or an empty string, 45 depending on the parser. 46 47 """ 48 49 def __init__(self, tag, attrib={}, **extra): 50 if not isinstance(attrib, dict): 51 raise TypeError("attrib must be dict, not %s" % ( 52 attrib.__class__.__name__,)) 53 attrib = attrib.copy() 54 attrib.update(extra) 55 self.tag = tag 56 self.attrib = attrib 57 self._children = [] 58 59 def __repr__(self): 60 return "<%s %r at %#x>" % (self.__class__.__name__, self.tag, id(self)) 61 62 def makeelement(self, tag, attrib): 63 建立一個新節點 64 """Create a new element with the same type. 65 66 *tag* is a string containing the element name. 67 *attrib* is a dictionary containing the element attributes. 68 69 Do not call this method, use the SubElement factory function instead. 70 71 """ 72 return self.__class__(tag, attrib) 73 74 def copy(self): 75 """Return copy of current element. 76 77 This creates a shallow copy. Subelements will be shared with the 78 original tree. 79 80 """ 81 elem = self.makeelement(self.tag, self.attrib) 82 elem.text = self.text 83 elem.tail = self.tail 84 elem[:] = self 85 return elem 86 87 def __len__(self): 88 return len(self._children) 89 90 def __bool__(self): 91 warnings.warn( 92 "The behavior of this method will change in future versions. " 93 "Use specific 'len(elem)' or 'elem is not None' test instead.", 94 FutureWarning, stacklevel=2 95 ) 96 return len(self._children) != 0 # emulate old behaviour, for now 97 98 def __getitem__(self, index): 99 return self._children[index] 100 101 def __setitem__(self, index, element): 102 # if isinstance(index, slice): 103 # for elt in element: 104 # assert iselement(elt) 105 # else: 106 # assert iselement(element) 107 self._children[index] = element 108 109 def __delitem__(self, index): 110 del self._children[index] 111 112 def append(self, subelement): 113 爲當前節點追加一個子節點 114 """Add *subelement* to the end of this element. 115 116 The new element will appear in document order after the last existing 117 subelement (or directly after the text, if it's the first subelement), 118 but before the end tag for this element. 119 120 """ 121 self._assert_is_element(subelement) 122 self._children.append(subelement) 123 124 def extend(self, elements): 125 爲當前節點擴展 n 個子節點 126 """Append subelements from a sequence. 127 128 *elements* is a sequence with zero or more elements. 129 130 """ 131 for element in elements: 132 self._assert_is_element(element) 133 self._children.extend(elements) 134 135 def insert(self, index, subelement): 136 在當前節點的子節點中插入某個節點,即:爲當前節點建立子節點,而後插入指定位置 137 """Insert *subelement* at position *index*.""" 138 self._assert_is_element(subelement) 139 self._children.insert(index, subelement) 140 141 def _assert_is_element(self, e): 142 # Need to refer to the actual Python implementation, not the 143 # shadowing C implementation. 144 if not isinstance(e, _Element_Py): 145 raise TypeError('expected an Element, not %s' % type(e).__name__) 146 147 def remove(self, subelement): 148 在當前節點在子節點中刪除某個節點 149 """Remove matching subelement. 150 151 Unlike the find methods, this method compares elements based on 152 identity, NOT ON tag value or contents. To remove subelements by 153 other means, the easiest way is to use a list comprehension to 154 select what elements to keep, and then use slice assignment to update 155 the parent element. 156 157 ValueError is raised if a matching element could not be found. 158 159 """ 160 # assert iselement(element) 161 self._children.remove(subelement) 162 163 def getchildren(self): 164 獲取全部的子節點(廢棄) 165 """(Deprecated) Return all subelements. 166 167 Elements are returned in document order. 168 169 """ 170 warnings.warn( 171 "This method will be removed in future versions. " 172 "Use 'list(elem)' or iteration over elem instead.", 173 DeprecationWarning, stacklevel=2 174 ) 175 return self._children 176 177 def find(self, path, namespaces=None): 178 獲取第一個尋找到的子節點 179 """Find first matching element by tag name or path. 180 181 *path* is a string having either an element tag or an XPath, 182 *namespaces* is an optional mapping from namespace prefix to full name. 183 184 Return the first matching element, or None if no element was found. 185 186 """ 187 return ElementPath.find(self, path, namespaces) 188 189 def findtext(self, path, default=None, namespaces=None): 190 獲取第一個尋找到的子節點的內容 191 """Find text for first matching element by tag name or path. 192 193 *path* is a string having either an element tag or an XPath, 194 *default* is the value to return if the element was not found, 195 *namespaces* is an optional mapping from namespace prefix to full name. 196 197 Return text content of first matching element, or default value if 198 none was found. Note that if an element is found having no text 199 content, the empty string is returned. 200 201 """ 202 return ElementPath.findtext(self, path, default, namespaces) 203 204 def findall(self, path, namespaces=None): 205 獲取全部的子節點 206 """Find all matching subelements by tag name or path. 207 208 *path* is a string having either an element tag or an XPath, 209 *namespaces* is an optional mapping from namespace prefix to full name. 210 211 Returns list containing all matching elements in document order. 212 213 """ 214 return ElementPath.findall(self, path, namespaces) 215 216 def iterfind(self, path, namespaces=None): 217 獲取全部指定的節點,並建立一個迭代器(能夠被for循環) 218 """Find all matching subelements by tag name or path. 219 220 *path* is a string having either an element tag or an XPath, 221 *namespaces* is an optional mapping from namespace prefix to full name. 222 223 Return an iterable yielding all matching elements in document order. 224 225 """ 226 return ElementPath.iterfind(self, path, namespaces) 227 228 def clear(self): 229 清空節點 230 """Reset element. 231 232 This function removes all subelements, clears all attributes, and sets 233 the text and tail attributes to None. 234 235 """ 236 self.attrib.clear() 237 self._children = [] 238 self.text = self.tail = None 239 240 def get(self, key, default=None): 241 獲取當前節點的屬性值 242 """Get element attribute. 243 244 Equivalent to attrib.get, but some implementations may handle this a 245 bit more efficiently. *key* is what attribute to look for, and 246 *default* is what to return if the attribute was not found. 247 248 Returns a string containing the attribute value, or the default if 249 attribute was not found. 250 251 """ 252 return self.attrib.get(key, default) 253 254 def set(self, key, value): 255 爲當前節點設置屬性值 256 """Set element attribute. 257 258 Equivalent to attrib[key] = value, but some implementations may handle 259 this a bit more efficiently. *key* is what attribute to set, and 260 *value* is the attribute value to set it to. 261 262 """ 263 self.attrib[key] = value 264 265 def keys(self): 266 獲取當前節點的全部屬性的 key 267 268 """Get list of attribute names. 269 270 Names are returned in an arbitrary order, just like an ordinary 271 Python dict. Equivalent to attrib.keys() 272 273 """ 274 return self.attrib.keys() 275 276 def items(self): 277 獲取當前節點的全部屬性值,每一個屬性都是一個鍵值對 278 """Get element attributes as a sequence. 279 280 The attributes are returned in arbitrary order. Equivalent to 281 attrib.items(). 282 283 Return a list of (name, value) tuples. 284 285 """ 286 return self.attrib.items() 287 288 def iter(self, tag=None): 289 在當前節點的子孫中根據節點名稱尋找全部指定的節點,並返回一個迭代器(能夠被for循環)。 290 """Create tree iterator. 291 292 The iterator loops over the element and all subelements in document 293 order, returning all elements with a matching tag. 294 295 If the tree structure is modified during iteration, new or removed 296 elements may or may not be included. To get a stable set, use the 297 list() function on the iterator, and loop over the resulting list. 298 299 *tag* is what tags to look for (default is to return all elements) 300 301 Return an iterator containing all the matching elements. 302 303 """ 304 if tag == "*": 305 tag = None 306 if tag is None or self.tag == tag: 307 yield self 308 for e in self._children: 309 yield from e.iter(tag) 310 311 # compatibility 312 def getiterator(self, tag=None): 313 # Change for a DeprecationWarning in 1.4 314 warnings.warn( 315 "This method will be removed in future versions. " 316 "Use 'elem.iter()' or 'list(elem.iter())' instead.", 317 PendingDeprecationWarning, stacklevel=2 318 ) 319 return list(self.iter(tag)) 320 321 def itertext(self): 322 在當前節點的子孫中根據節點名稱尋找全部指定的節點的內容,並返回一個迭代器(能夠被for循環)。 323 """Create text iterator. 324 325 The iterator loops over the element and all subelements in document 326 order, returning all inner text. 327 328 """ 329 tag = self.tag 330 if not isinstance(tag, str) and tag is not None: 331 return 332 if self.text: 333 yield self.text 334 for e in self: 335 yield from e.itertext() 336 if e.tail: 337 yield e.tail 338 339 節點功能一覽表
建立節點並附加到原文件節點的實例:
#!usr/bin/env python # -*- coding:utf-8 -*- from xml.etree import ElementTree as ET tree = ET.parse('first.xml') root = tree.getroot() new_root = root.makeelement('name',{'age':'28'}) #在內存建立一個節點,該節點與root沒有關係 root.append(new_root) #將建立的節點做爲子節點附加到root tree.write('first.xml') #將內存中的節點寫入文件
#!usr/bin/env python # -*- coding:utf-8 -*- #!usr/bin/env python # -*- coding:utf-8 -*- from xml.etree import ElementTree as ET tree = ET.parse('first.xml') root = tree.getroot() new_root = root.makeelement('name',{'age':'28'}) #在內存建立一個節點,該節點與root沒有關係 new_root1 = ET.Element('name1',{'age1':'38'}) #利用類建立一個節點,上面的代碼實質上是調用了這句 new_root.append(new_root1) #new_root1作爲new_root的子節點 root.append(new_root) tree.write('first.xml',encoding='utf-8',xml_declaration=True,short_empty_elements=False) #將內存中的節點寫入文件,參數utf-8支持中文, # xml_declaration=True文件頭部加上註釋, # short_empty_elements=False節點沒有值時也全閉合,例如:<country name="Panama"></country>
給xml文件添加縮進:
#!usr/bin/env python # -*- coding:utf-8 -*- from xml.etree import ElementTree as ET root = ET.Element('data') son_root1 = ET.SubElement(root,'country',{'name':'Liechtenstein'}) son_root2 = ET.SubElement(root,'country',{'name':'Singapore'}) son_root3 = ET.SubElement(root,'country',{'name':'Panama'}) son_root1_son1 = ET.SubElement(son_root1,'rank',{'updated':'yes'}) son_root1_son1.text = '2' son_root1_son1 = ET.SubElement(son_root1,'year') son_root1_son1.text = '2025' son_root1_son1 = ET.SubElement(son_root1,'gdppc') son_root1_son1.text = '141100' son_root1_son1 = ET.SubElement(son_root1,'neighbor',attrib = {'direction':'B','name':'Austria'}) son_root1_son1.text = '112233' son_root1_son1 = ET.SubElement(son_root1,'neighbor',attrib = {'direction':'W','name':'Switzerland'}) # et = ET.ElementTree(new_xml) # et.write('test.xml',encoding='utf-8',xml_declaration=True,short_empty_elements=False) #如下爲將xml字符串轉換爲有縮進的字符串 #minidom功能少,效率低,通常咱們用ElementTree,但minidom有美化縮進xml的功能 from xml.dom import minidom root_string = ET.tostring(root,encoding='utf-8') #將ET節點轉化爲字符串 reparsed = minidom.parseString(root_string) #利用minidom將字符串轉化爲xml.dom.minidom.Document對象 pretty = reparsed.toprettyxml(indent='\t') #利用minidom函數轉化爲有縮進格式的xml with open('first.xml','w',encoding='utf-8') as f: f.write(pretty)
XML命名空間:
XML 命名空間提供避免元素命名衝突的方法。 命名衝突 在 XML 中,元素名稱是由開發者定義的,當兩個不一樣的文檔使用相同的元素名時,就會發生命名衝突。 這個 XML 文檔攜帶着某個表格中的信息: <table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> 這個 XML 文檔攜帶有關桌子的信息(一件傢俱): <table> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table> 假如這兩個 XML 文檔被一塊兒使用,因爲兩個文檔都包含帶有不一樣內容和定義的 <table> 元素,就會發生命名衝突。 XML 解析器沒法肯定如何處理這類衝突。 使用前綴來避免命名衝突 此文檔帶有某個表格中的信息: <h:table> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> 此 XML 文檔攜帶着有關一件傢俱的信息: <f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> 如今,命名衝突不存在了,這是因爲兩個文檔都使用了不一樣的名稱來命名它們的 <table> 元素 (<h:table> 和 <f:table>)。 經過使用前綴,咱們建立了兩種不一樣類型的 <table> 元素。 使用命名空間(Namespaces) 這個 XML 文檔攜帶着某個表格中的信息: <h:table xmlns:h="http://www.w3.org/TR/html4/"> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> 此 XML 文檔攜帶着有關一件傢俱的信息: <f:table xmlns:f="http://www.w3school.com.cn/furniture"> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> 與僅僅使用前綴不一樣,咱們爲 <table> 標籤添加了一個 xmlns 屬性,這樣就爲前綴賦予了一個與某個命名空間相關聯的限定名稱。 XML Namespace (xmlns) 屬性 XML 命名空間屬性被放置於元素的開始標籤之中,並使用如下的語法: xmlns:namespace-prefix="namespaceURI" 當命名空間被定義在元素的開始標籤中時,全部帶有相同前綴的子元素都會與同一個命名空間相關聯。 註釋:用於標示命名空間的地址不會被解析器用於查找信息。其唯一的做用是賦予命名空間一個唯一的名稱。不過,不少公司經常會做爲指針來使用命名空間指向實際存在的網頁,這個網頁包含關於命名空間的信息。 請訪問 http://www.w3.org/TR/html4/。 統一資源標識符(Uniform Resource Identifier (URI)) 統一資源標識符是一串能夠標識因特網資源的字符。最經常使用的 URI 是用來標示因特網域名地址的統一資源定位器(URL)。另外一個不那麼經常使用的 URI 是統一資源命名(URN)。在咱們的例子中,咱們僅使用 URL。 默認的命名空間(Default Namespaces) 爲元素定義默認的命名空間可讓咱們省去在全部的子元素中使用前綴的工做。 請使用下面的語法: xmlns="namespaceURI" 這個 XML 文檔攜帶着某個表格中的信息: <table xmlns="http://www.w3.org/TR/html4/"> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> 此 XML 文檔攜帶着有關一件傢俱的信息: <table xmlns="http://www.w3school.com.cn/furniture"> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table> 命名空間的實際應用 當開始使用 XSL 時,您不久就會看到實際使用中的命名空間。XSL 樣式表用於將 XML 文檔轉換爲其餘格式,好比 HTML。 若是您仔細觀察下面的這個 XSL 文檔,就會看到大多數的標籤是HTML標籤。非 HTML 的標籤都有前綴 xsl,並由此命名空間標示:"http://www.w3.org/1999/XSL/Transform": <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr> <th align="left">Title</th> <th align="left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
命名空間實例:
#!usr/bin/env python # -*- coding:utf-8 -*- #******************************************************************************************* # <f:table xmlns:f="http://www.w3school.com.cn/furniture"> # <f:name>African Coffee Table</f:name> # <f:width hhh="123">80</f:width> # <f:length>120</f:length> # </f:table #******************************************************************************************* from xml.etree import ElementTree as ET ET.register_namespace('f','http://www.w3school.com.cn/furniture') # 建立節點 root = ET.Element('{http://www.w3school.com.cn/furniture}table') root_son1 = ET.SubElement(root,'{http://www.w3school.com.cn/furniture}name') root_son1.text = 'African Coffee Table' root_son2 = ET.SubElement(root,'{http://www.w3school.com.cn/furniture}width',attrib={'{http://www.w3school.com.cn/furniture}hh':'123'}) root_son2.text = '80' root_son3 = ET.SubElement(root,'{http://www.w3school.com.cn/furniture}lenth') root_son3.text = '120' root_str = ET.tostring(root) from xml.dom import minidom reparse = minidom.parseString(root_str) pretty = reparse.toprettyxml(indent='\t') with open('abc.xml','w',encoding='utf-8') as f: f.write(pretty)
XML接口處理實例:
11、configparser模塊
configparser用於處理特定格式的文件,其本質上是利用open來操做文件
# 註釋1 ; 註釋2 [section1] # 節點 k1 = v1 # 值 k2:v2 # 值 [section2] # 節點 k1 = v1 # 值
1、獲取全部節點 import configparser config = configparser.ConfigParser() config.read('xxxooo', encoding='utf-8') ret = config.sections() print(ret) 2、獲取指定節點下全部的鍵值對 import configparser config = configparser.ConfigParser() config.read('xxxooo', encoding='utf-8') ret = config.items('section1') print(ret) 3、獲取指定節點下全部的建 import configparser config = configparser.ConfigParser() config.read('xxxooo', encoding='utf-8') ret = config.options('section1') print(ret) 4、獲取指定節點下指定key的值 import configparser config = configparser.ConfigParser() config.read('xxxooo', encoding='utf-8') v = config.get('section1', 'k1') # v = config.getint('section1', 'k1') # v = config.getfloat('section1', 'k1') # v = config.getboolean('section1', 'k1') print(v) 5、檢查、刪除、添加節點 import configparser config = configparser.ConfigParser() config.read('xxxooo', encoding='utf-8') # 檢查 has_sec = config.has_section('section1') print(has_sec) # 添加節點 config.add_section("SEC_1") config.write(open('xxxooo', 'w')) # 刪除節點 config.remove_section("SEC_1") config.write(open('xxxooo', 'w')) 6、檢查、刪除、設置指定組內的鍵值對 import configparser config = configparser.ConfigParser() config.read('xxxooo', encoding='utf-8') # 檢查 has_opt = config.has_option('section1', 'k1') print(has_opt) # 刪除 config.remove_option('section1', 'k1') config.write(open('xxxooo', 'w')) # 設置 config.set('section1', 'k10', "123") config.write(open('xxxooo', 'w'))
高級的 文件、文件夾、壓縮包 處理模塊
shutil.copyfileobj(fsrc, fdst[, length])
將文件內容拷貝到另外一個文件中
1
2
3
|
import
shutil
shutil.copyfileobj(
open
(
'old.xml'
,
'r'
),
open
(
'new.xml'
,
'w'
))
|
shutil.copyfile(src, dst)
拷貝文件
1
|
shutil.copyfile(
'f1.log'
,
'f2.log'
)
|
shutil.copymode(src, dst)
僅拷貝權限。內容、組、用戶均不變
1
|
shutil.copymode(
'f1.log'
,
'f2.log'
)
|
shutil.copystat(src, dst)
僅拷貝狀態的信息,包括:mode bits, atime, mtime, flags
1
|
shutil.copystat(
'f1.log'
,
'f2.log'
)
|
shutil.copy(src, dst)
拷貝文件和權限
1
2
3
|
import
shutil
shutil.copy(
'f1.log'
,
'f2.log'
)
|
shutil.copy2(src, dst)
拷貝文件和狀態信息
1
2
3
|
import
shutil
shutil.copy2(
'f1.log'
,
'f2.log'
)
|
shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
遞歸的去拷貝文件夾
1
2
3
|
import
shutil
shutil.copytree(
'folder1'
,
'folder2'
, ignore
=
shutil.ignore_patterns(
'*.pyc'
,
'tmp*'
))
|
import shutil shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
#symlinks=True 是拷貝快捷方式,仍是拷貝快捷方式對應的原文件
shutil.rmtree(path[, ignore_errors[, onerror]])
遞歸的去刪除文件
1
2
3
|
import
shutil
shutil.rmtree(
'folder1'
)
|
shutil.move(src, dst)
遞歸的去移動文件,它相似mv命令,其實就是重命名。
1
2
3
|
import
shutil
shutil.move(
'folder1'
,
'folder3'
)
|
shutil.make_archive(base_name, format,...)
建立壓縮包並返回文件路徑,例如:zip、tar
建立壓縮包並返回文件路徑,例如:zip、tar
1
2
3
4
5
6
7
8
|
#將 /Users/wupeiqi/Downloads/test 下的文件打包放置當前程序目錄
import
shutil
ret
=
shutil.make_archive(
"wwwwwwwwww"
,
'gztar'
, root_dir
=
'/Users/wupeiqi/Downloads/test'
)
#將 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目錄
import
shutil
ret
=
shutil.make_archive(
"/Users/wupeiqi/wwwwwwwwww"
,
'gztar'
, root_dir
=
'/Users/wupeiqi/Downloads/test'
)
|
shutil 對壓縮包的處理是調用 ZipFile 和 TarFile 兩個模塊來進行的,詳細:
import zipfile # 壓縮 z = zipfile.ZipFile('laxi.zip', 'w') z.write('a.log') z.write('data.data') z.close() # 解壓 z = zipfile.ZipFile('laxi.zip', 'r') z.extractall() z.close()
import tarfile # 壓縮 tar = tarfile.open('your.tar','w') tar.add('/Users/wupeiqi/PycharmProjects/bbs2.log', arcname='bbs2.log') tar.add('/Users/wupeiqi/PycharmProjects/cmdb.log', arcname='cmdb.log') tar.close() # 解壓 tar = tarfile.open('your.tar','r') tar.extractall() # 可設置解壓地址 tar.close()
十3、subprocess模塊
執行shell命令的相關的模塊和函數的功能均在 subprocess 模塊中實現,並提供了更豐富的功能。
call
執行命令,返回狀態碼
1
2
|
ret
=
subprocess.call([
"ls"
,
"-l"
], shell
=
False
)
ret
=
subprocess.call(
"ls -l"
, shell
=
True
)
|
check_call
執行命令,若是執行狀態碼是 0 ,則返回0,不然拋異常
1
2
|
subprocess.check_call([
"ls"
,
"-l"
])
subprocess.check_call(
"exit 1"
, shell
=
True
)
|
check_output
執行命令,若是狀態碼是 0 ,則返回執行結果,不然拋異常
1
2
|
subprocess.check_output([
"echo"
,
"Hello World!"
])
subprocess.check_output(
"exit 1"
, shell
=
True
)
|
subprocess.Popen(...)
用於執行復雜的系統命令
參數:
import subprocess ret1 = subprocess.Popen(["mkdir","t1"]) ret2 = subprocess.Popen("mkdir t2", shell=True)
終端輸入的命令分爲兩種:
import subprocess obj = subprocess.Popen("mkdir t3", shell=True, cwd='/home/dev',)
import subprocess obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) obj.stdin.write("print(1)\n") obj.stdin.write("print(2)") out_error_list = obj.communicate() print(out_error_list)
import subprocess obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) out_error_list = obj.communicate('print("hello")') print(out_error_list)
十4、logging模塊
用於便捷記錄日誌且線程安全的模塊,多人能夠同時打開保存,系統會控制好隊列
一、寫單文件日誌
#!usr/bin/env python # -*- coding:utf-8 -*- import logging logging.basicConfig(filename='log.log', format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s', level=10) #當設置level=10時,下面的級別>=10時才寫入日誌 logging.debug('debug') logging.info('info') logging.warning('warning') logging.error('erro') logging.critical('critical')
注:只有【當前寫等級】大於【日誌等級】時,日誌文件才被記錄。
定義handler的輸出格式formatter。輸出格式有不少。
format參數中可能用到的格式化串:
%(name)s Logger的名字
%(levelno)s 數字形式的日誌級別
%(levelname)s 文本形式的日誌級別
%(filename)s 調用日誌輸出函數的模塊的文件名
%(module)s 調用日誌輸出函數的模塊名
%(funcName)s 調用日誌輸出函數的函數名
%(lineno)d 調用日誌輸出函數的語句所在的代碼行
%(created)f 當前時間,用UNIX標準的表示時間的浮 點數表示
%(relativeCreated)d 輸出日誌信息時的,自Logger建立以 來的毫秒數
%(asctime)s 字符串形式的當前時間。默認格式是 「2003-07-08 16:49:45,896」。逗號後面的是毫秒
%(message)s用戶輸出的消息
二、多文件日誌
對於上述記錄日誌的功能,只能將日誌記錄在單文件中,若是想要設置多個日誌文件,logging.basicConfig將沒法完成,須要自定義文件和日誌操做對象。
#!usr/bin/env python # -*- coding:utf-8 -*- import logging #*************定義文件*********************************************************************************** #實例化一個FileHander對象,該對象是將格式化後的日誌記錄寫入硬盤 file_1_1 = logging.FileHandler('11_1.log','a',encoding='utf-8') #實例化一個FileHander對象 #實例化一個Formatter對象,該對象是將日誌記錄轉化爲文本 fmt = logging.Formatter(fmt='%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s') file_1_1.setFormatter(fmt) #將file_1_1的格式設置爲fmt file_1_2 = logging.FileHandler('11_2.log','a',encoding='utf-8') fmt = logging.Formatter(fmt='%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s') file_1_2.setFormatter(fmt) #********************************************************************************************************** #*************定義日誌************************************************************************************* #實例化一個Logger對象,做用:初始化一個logging對象 logger1 = logging.Logger('s1',level=logging.ERROR) logger1.addHandler(file_1_1) #將規定的handler加入日誌 logger1.addHandler(file_1_2) #********************************************************************************************************** #*************寫入日誌************************************************************************************* logger1.critical('123')
固然也能夠用自定義的方法記錄到一個日誌文件中:
#!usr/bin/env python # -*- coding:utf-8 -*- import logging #定義文件 file_2_1 = logging.FileHandler('21-1.log','a',encoding='utf-8') fmt = logging.Formatter(fmt='%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s') file_2_1.setFormatter(fmt) #定義日誌 logger2 = logging.Logger('s2',level=10) logger2.addHandler(file_2_1) #寫入日誌 logger2.info('記錄一個info錯誤')