本節大綱:html
模塊,用一砣代碼實現了某個功能的代碼集合。 java
相似於函數式編程和麪向過程編程,函數式編程則完成一個功能,其餘代碼用來調用便可,提供了代碼的重用性和代碼間的耦合。而對於一個複雜的功能來,可能須要多個函數才能完成(函數又能夠在不一樣的.py文件中),n個 .py 文件組成的代碼集合就稱爲模塊。python
如:os 是系統相關的模塊;file是文件操做相關的模塊linux
模塊分爲三種:git
自定義模塊 和開源模塊的使用參考 http://www.cnblogs.com/wupeiqi/articles/4963027.html正則表達式
import time # print(time.time()/60/60/24/365) #時間戳:秒 從1970年1月份開始 # print(time.altzone/60/60) #返回與utc時間的時間差,以秒計算\ # print(time.asctime()) #返回時間格式"Fri Aug 19 11:14:16 2016", # # print(time.localtime()) #返回本地時間 的struct time對象格式 tm_wday=6 週日是第6提天,周1是第0天 # t = time.localtime() # print(t.tm_year,t.tm_mon) #能夠自定義要顯示的內容 # t1 = time.localtime(time.time()-(60*60*24)) #減去一天的時間 # print(t1) # # print(time.ctime()) #返回Fri Aug 19 12:38:29 2016 格式, 同上 #自定義展現時間格式 # print(time.strftime('%Y-%m-%d %H:%M:%S')) # struct_time=time.localtime(time.time()-86400) # print(time.strftime('%Y-%m-%d %H:%M:%S',struct_time)) #獲取前一臺的時間 # # 日期字符串 轉成 時間戳 # print(time.strptime('2017-02-23','%Y-%m-%d')) #把字符串轉成時間對象 # t = time.strptime('2017-02-23','%Y-%m-%d') # print(time.mktime(t)) #把字符串轉換成時間戳 #將時間戳轉爲字符串格式 # print(time.gmtime(time.time()-86640)) #將utc時間戳轉換成struct_time格式 # print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #將utc struct_time格式轉成指定的字符串格式 # #時間加減 # import datetime # print(datetime.datetime.now()) #打印當前日期 # 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分 # # c_time = datetime.datetime.now() # print(c_time.replace(minute=3,hour=2)) #時間替換
Directive | Meaning | Notes |
---|---|---|
%a |
Locale’s abbreviated weekday name. | |
%A |
Locale’s full weekday name. | |
%b |
Locale’s abbreviated month name. | |
%B |
Locale’s full month name. | |
%c |
Locale’s appropriate date and time representation. | |
%d |
Day of the month as a decimal number [01,31]. | |
%H |
Hour (24-hour clock) as a decimal number [00,23]. | |
%I |
Hour (12-hour clock) as a decimal number [01,12]. | |
%j |
Day of the year as a decimal number [001,366]. | |
%m |
Month as a decimal number [01,12]. | |
%M |
Minute as a decimal number [00,59]. | |
%p |
Locale’s equivalent of either AM or PM. | (1) |
%S |
Second as a decimal number [00,61]. | (2) |
%U |
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. | (3) |
%w |
Weekday as a decimal number [0(Sunday),6]. | |
%W |
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. | (3) |
%x |
Locale’s appropriate date representation. | |
%X |
Locale’s appropriate time representation. | |
%y |
Year without century as a decimal number [00,99]. | |
%Y |
Year with century as a decimal number. | |
%z |
Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. | |
%Z |
Time zone name (no characters if no time zone exists). | |
%% |
A literal '%' character. |
# -*- coding: UTF-8 -*- #blog:http://www.cnblogs.com/linux-chenyang/ #random ,string模塊 import random print(random.random()) #生成隨機數 print(random.randint(1,10)) #1~10之內隨機生成一個數 print(random.randrange(1,20,2)) #步長爲2,永遠顯示奇數 print(random.sample([1,2,3,4,5,6,63,23,543],2)) #隨機取兩個值 print(random.sample(range(100),5)) #生成隨機驗證碼 import random,string print(string.digits) #顯示全部的數字 print(string.ascii_letters) #顯示全部的英文字母 print(string.ascii_lowercase) #顯示小寫英文字母 print(string.ascii_uppercase) #顯示大小英文字符 souce = string.digits+string.ascii_letters print(souce) print(''.join(random.sample(souce,6))) #生成隨機驗證碼2 import random checkcode = '' for i in range(4): current = random.randrange(0,4) if current != i: temp = chr(random.randint(65,90)) else: temp = random.randint(0,9) checkcode += str(temp) print(checkcode)
os.getcwd() 獲取當前工做目錄,即當前python腳本工做的目錄路徑 os.chdir("dirname") 改變當前腳本工做目錄;至關於shell下cd os.curdir 返回當前目錄: ('.') os.pardir 獲取當前目錄的父目錄字符串名:('..') os.makedirs('dirname1/dirname2') 可生成多層遞歸目錄 os.removedirs('dirname1') 若目錄爲空,則刪除,並遞歸到上一級目錄,如若也爲空,則刪除,依此類推 os.mkdir('dirname') 生成單級目錄;至關於shell中mkdir dirname os.rmdir('dirname') 刪除單級空目錄,若目錄不爲空則沒法刪除,報錯;至關於shell中rmdir dirname os.listdir('dirname') 列出指定目錄下的全部文件和子目錄,包括隱藏文件,並以列表方式打印 os.remove() 刪除一個文件 os.rename("oldname","newname") 重命名文件/目錄 os.stat('path/filename') 獲取文件/目錄信息 os.sep 輸出操做系統特定的路徑分隔符,win下爲"\\",Linux下爲"/" os.linesep 輸出當前平臺使用的行終止符,win下爲"\t\n",Linux下爲"\n" os.pathsep 輸出用於分割文件路徑的字符串 os.name 輸出字符串指示當前使用平臺。win->'nt'; Linux->'posix' os.system("bash command") 運行shell命令,直接顯示 os.environ 獲取系統環境變量 os.path.abspath(path) 返回path規範化的絕對路徑 os.path.split(path) 將path分割成目錄和文件名二元組返回 os.path.dirname(path) 返回path的目錄。其實就是os.path.split(path)的第一個元素 os.path.basename(path) 返回path最後的文件名。如何path以/或\結尾,那麼就會返回空值。即os.path.split(path)的第二個元素 os.path.exists(path) 若是path存在,返回True;若是path不存在,返回False os.path.isabs(path) 若是path是絕對路徑,返回True os.path.isfile(path) 若是path是一個存在的文件,返回True。不然返回False os.path.isdir(path) 若是path是一個存在的目錄,則返回True。不然返回False os.path.join(path1[, path2[, ...]]) 將多個路徑組合後返回,第一個絕對路徑以前的參數將被忽略 os.path.getatime(path) 返回path所指向的文件或者目錄的最後存取時間 os.path.getmtime(path) 返回path所指向的文件或者目錄的最後修改時間
高級的 文件、文件夾、壓縮包 處理模塊shell
# -*- coding: UTF-8 -*- #blog:http://www.cnblogs.com/linux-chenyang/ import shutil #shutil.copyfileobj(fsrc, fdst[, length]) fsrc和fdst表示對象,不是文件名 #f = open("testfile.log") #f1 = open("testnew.log","w") #shutil.copyfileobj(f,f1) #shutil.copyfile(src, dst) 直接輸入文件名copy #shutil.copyfile(r"D:\pycharm\s16\day5\access.log","testnew2.log") #shutil.copymode(src, dst) 僅拷貝權限。內容、組、用戶均不變 #shutil.copy(src, dst) 拷貝文件和權限 #shutil.copy2(src, dst) 拷貝文件和狀態信息 #shutil.ignore_patterns(*patterns) #shutil.copytree(src, dst, symlinks=False, ignore=None) #遞歸的去拷貝文件 #例如:copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*')) #能夠過濾目錄和指定的文件 #shutil.copytree("D:\\pycharm\s16\day5",'D:\\pycharm\s16\day6\Test',ignore=shutil.ignore_patterns('目錄','access.log')) #shutil.rmtree(path[, ignore_errors[, onerror]]) 遞歸的去刪除文件,目錄,不能指定過濾條件 #shutil.rmtree('D:\\pycharm\s16\day6\Test') #shutil.move(src, dst) 遞歸的去移動文件
shutil.make_archive(base_name, format,...)編程
建立壓縮包並返回文件路徑,例如:zip、tarjson
#壓縮一個zip文件 shutil.make_archive('day5', 'zip','D:\\pycharm\s16\day5')
shutil 對壓縮包的處理是調用 ZipFile 和 TarFile 兩個模塊來進行的,詳細:windows
#壓縮 z = zipfile.ZipFile('lijun.zip','w') z.write("D:\\pycharm\s16\day5\ccess.log",arcname="ccess.log") #不要目錄 z.write("D:\\pycharm\s16\day6") z.write('類.py') z.close() #解壓 z = zipfile.ZipFile('lijun.zip', 'r') z.extractall() #()裏面能夠添加文件名,只解壓一個 z.extract(path="C:\\zip3") #指定解壓路徑和名字 z.close()
import tarfile # 壓縮 tar = tarfile.open('your.tar','w') tar.add('/Users/wupeiqi/PycharmProjects/bbs2.zip', arcname='bbs2.zip') tar.add('/Users/wupeiqi/PycharmProjects/cmdb.zip', arcname='cmdb.zip') tar.close() # 解壓 tar = tarfile.open('your.tar','r') tar.extractall() # 可設置解壓地址 tar.close()
用於序列化的兩個模塊
Json模塊提供了四個功能:dumps、dump、loads、load
pickle模塊提供了四個功能:dumps、dump、loads、load
pickle運用場景:遊戲的存檔,虛擬機的快照
# -*- coding: UTF-8 -*- #blog:http://www.cnblogs.com/linux-chenyang/ #數據是個字典 #1.往文件裏寫東西,只能傳人字符串和bytes格式,像數字,字典,列表都是內存的數據類型,只能在內存裏用,不能在硬盤上寫。 #解決方法:字典存的時候先用str()函數轉成字符串的形式,讀的時候在用eval()函數轉成字典 # #把內存數據類型轉成字符串這就叫數據的序列化 #從字符串轉成內存裏的數據類型叫反序列化 # #pickle就是專門用於以上場景:必須是字節的形式 #案例:在一個腳本里寫,來另一個腳本里讀,文件的名字叫accoun.db import pickle account = { 'id':622202020011458, 'credit':50000, 'balance':8000, 'expire_date':"2020-5-21", 'passwd':'123455' } f = open("account.db","wb") f.write(pickle.dumps(account)) #序列化 #pickle.dump(account.f) f.close() #################### # -*- coding: UTF-8 -*- #blog:http://www.cnblogs.com/linux-chenyang/ import pickle f = open('account.db',"rb") account = pickle.loads(f.read()) #反序列化 #pickle.load(f) print(account) #輸出: {'credit': 50000, 'balance': 8000, 'passwd': '123455', 'expire_date': '2020-5-21', 'id': 622202020011458}
json的用法和pickie用法同樣,只不過要將wb和rb的方式改爲w和r
#案例:在一個腳本里寫,來另一個腳本里讀,文件的名字叫accoun.db import json as pickle account = { 'id':622202020011458, 'credit':50000, 'balance':8000, 'expire_date':"2020-5-21", 'passwd':'123455' } f = open("account.db","w") f.write(pickle.dumps(account)) #序列化 #pickle.dump(account.f) f.close() ######## import json as pickle f = open('account.db',"r") account = pickle.loads(f.read()) #反序列化 #pickle.load(f) print(account) #輸出:這時候account.db裏的內容也直接能夠顯示,pickle裏的account.db內容是顯示不了的 {'expire_date': '2020-5-21', 'balance': 8000, 'credit': 50000, 'passwd': '123455', 'id': 622202020011458}
pickle和json的區別:
用於生成和修改常見配置文檔,當前模塊的名稱在 python 3.x 版本中變動爲 configparser。
來看一個好多軟件的常見文檔格式以下
[DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecret.server.com] Port = 50022 ForwardX11 = no
若是想用python生成一個這樣的文檔怎麼作呢?
import configparser config = configparser.ConfigParser() config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'} config['bitbucket.org'] = {} config['bitbucket.org']['User'] = 'hg' config['topsecret.server.com'] = {} topsecret = config['topsecret.server.com'] topsecret['Host Port'] = '50022' # mutates the parser topsecret['ForwardX11'] = 'no' # same here config['DEFAULT']['ForwardX11'] = 'yes' with open('example.ini', 'w') as configfile: config.write(configfile)
寫完了還能夠再讀出來哈。
>>> import configparser >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: print(key) ... user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'
configparser增刪改查語法
[section1] k1 = v1 k2:v2 [section2] k1 = v1 import ConfigParser config = ConfigParser.ConfigParser() config.read('i.cfg') # ########## 讀 ########## #secs = config.sections() #print secs #options = config.options('group2') #print options #item_list = config.items('group2') #print item_list #val = config.get('group1','key') #val = config.getint('group1','key') # ########## 改寫 ########## #sec = config.remove_section('group1') #config.write(open('i.cfg', "w")) #sec = config.has_section('wupeiqi') #sec = config.add_section('wupeiqi') #config.write(open('i.cfg', "w")) #config.set('group2','k1',11111) #config.write(open('i.cfg', "w")) #config.remove_option('group2','age') #config.write(open('i.cfg', "w"))
subprocess – 建立附加進程
subprocess模塊提供了一種一致的方法來建立和處理附加進程,與標準庫中的其它模塊相比,提供了一個更高級的接口。用於替換以下模塊:
os.system() , os.spawnv() , os和popen2模塊中的popen()函數,以及 commands().
1.subprocess.run的用法
#每執行一個subprocess命令,就想當於啓動了一個進程,啓動了一個終端執行命令 >>> subprocess.run("df") Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 4128448 779452 3139284 20% / tmpfs 6092252 0 6092252 0% /dev/shm /dev/sda7 379731360 342792 360099276 1% /data0 /dev/sda5 8256952 154020 7683504 2% /tmp /dev/sda3 12385456 3583720 8172592 31% /usr /dev/sda6 8256952 985492 6852032 13% /var CompletedProcess(args='df', returncode=0) >>> subprocess.run(['df','-h']) #實際是個列表的形式,假如直接寫入df -h是會報錯 Filesystem Size Used Avail Use% Mounted on /dev/sda2 4.0G 762M 3.0G 20% / tmpfs 5.9G 0 5.9G 0% /dev/shm /dev/sda7 363G 335M 344G 1% /data0 /dev/sda5 7.9G 151M 7.4G 2% /tmp /dev/sda3 12G 3.5G 7.8G 31% /usr /dev/sda6 7.9G 963M 6.6G 13% /var CompletedProcess(args=['df', '-h'], returncode=0) >>> subprocess.run(['df','-h','|','grep','/dev/sda7']) #遇到有管道符的這種,這樣的方式能夠執行出結果,可是會報錯 df: `|': No such file or directory df: `grep': No such file or directory Filesystem Size Used Avail Use% Mounted on /dev/sda7 363G 335M 344G 1% /data0 CompletedProcess(args=['df', '-h', '|', 'grep', '/dev/sda7'], returncode=1) >>> subprocess.run("df -h | grep /dev/sda7",shell=True) #利用這種方法能夠解決管道符報錯的問題 /dev/sda7 363G 335M 344G 1% /data0 CompletedProcess(args='df -h | grep /dev/sda7', returncode=0) >>> a = subprocess.run("df -h | grep /dev/sda7",shell=True) #只會保存命令的執行狀態,不會保存具體輸出的內容 /dev/sda7 363G 335M 344G 1% /data0 >>> print(a) CompletedProcess(args='df -h | grep /dev/sda7', returncode=0) >>> a.returncode 0
2.subprocess.call,執行命令,執行命令,返回命令執行狀態 , 0 or 非0
>>> subprocess.call("df -h | grep /dev/sda7",shell=True) /dev/sda7 363G 335M 344G 1% /data0 0
3.subprocess.check_all,執行命令,若是命令結果爲0,就正常返回,不然拋異常
>>> a = subprocess.check_all("df -ssss | grep /dev/sda7",shell=True) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'subprocess' has no attribute 'check_all'
4.subprocess.getstatusoutput,接收字符串格式命令,返回元組形式,第1個元素是執行狀態,第2個是命令結果
>>> subprocess.getstatusoutput("df -h | grep /dev/sda7") #不用加shell參數的命令也能夠正常執行 (0, '/dev/sda7 363G 335M 344G 1% /data0')
5.subprocess.getoutput,接收字符串格式命令,並返回結果
>>> subprocess.getoutput("df -h | grep /dev/sda7") #結果 '/dev/sda7 363G 335M 344G 1% /data0'
6.subprocess.check_output,執行命令,並返回結果,注意是返回結果,不是打印,下例結果返回給res
>>> res=subprocess.check_output("df -h | grep /dev/sda7",shell=True) #直接輸出的就是結果 >>> res b'/dev/sda7 363G 335M 344G 1% /data0\n'
#上面那些方法,底層都是封裝的subprocess.Popen
poll()
Check if child process has terminated. Returns returncode
wait()
Wait for child process to terminate. Returns returncode attribute.
terminate() 殺掉所啓動進程
communicate() 等待任務結束
stdin 標準輸入
stdout 標準輸出
stderr 標準錯誤
pid
The process ID of the child process.
>>> res=subprocess.Popen("df -hT | grep /dev/sda7",shell=True,stdout=subprocess.PIPE) #兩個進程之間是不能直接通訊的,python和shell,第一條命令就至關於創建個管道,將命令輸出過去,結果在傳過來 >>> res.stdout.read() b'/dev/sda7 ext4 363G 335M 344G 1% /data0\n' >>> res=subprocess.Popen("df -hT | grep /dev/sda7",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) #假若有錯誤的信息也想傳過來,就加stderr=subprocess.PIPE >>> res.stdout.read() b'/dev/sda7 ext4 363G 335M 344G 1% /data0\n'
例子:
1.poll()和wait()的用法
#執行一個命令,不知道什麼時間纔會執行完,要是直接read讀的時候的話就會卡住。能夠先用poll()看下命令是否執行完,而後在去read >>> res=subprocess.Popen("top -bn 5",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) >>> print(res.poll()) None >>> print(res.poll()) #已經執行完 0 >>> res.stdout.read() #這時就不會卡了,直接能夠看到輸出的結果 >>> print(res.wall()) #只是顯示命令的執行狀態
2.terminate()和communicate()的用法
>>> res=subprocess.Popen("df -h | grep /dev/sda7;sleep 10",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) #執行一個時間長的命令 >>> res.poll() #查看命令好沒有執行完 >>> res.terminate() #殺掉進程 >>> res.poll() -15 >>> res.stdout.read() #而後立馬就能夠看到結果 b'/dev/sda7 363G 335M 344G 1% /data0\n' #communicate()的應用場景,假如我備份一個東西,我以爲半個小時的時間就能夠搞定,假如超過這個時間我就認爲是異常了,不用在哪傻等了,開始排錯 >>> res=subprocess.Popen("df -h | grep /dev/sda7;sleep 100",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) >>> res.communicate(timeout=2) #執行這個命令會卡這,2秒後纔會輸出結果 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.5/subprocess.py", line 1068, in communicate stdout, stderr = self._communicate(input, endtime, timeout) File "/usr/local/lib/python3.5/subprocess.py", line 1699, in _communicate self._check_timeout(endtime, orig_timeout) File "/usr/local/lib/python3.5/subprocess.py", line 1094, in _check_timeout raise TimeoutExpired(self.args, orig_timeout) subprocess.TimeoutExpired: Command 'df -h | grep /dev/sda7;sleep 100' timed out after 2 seconds
可用參數:
不少程序都有記錄日誌的需求,而且日誌中包含的信息即有正常的程序訪問日誌,還可能有錯誤、警告等信息輸出,python的logging模塊提供了標準的日誌接口,你能夠經過它存儲各類格式的日誌,logging的日誌能夠分爲 debug()
, info()
, warning()
, error()
and critical() 5個級別,
下面咱們看一下怎麼用。
最簡單的用法:
import logging logging.warning("user [alex] attempted wrong password more than 3 times") logging.critical("server is down") #輸出 WARNING:root:user [alex] attempted wrong password more than 3 times CRITICAL:root:server is down
看一下這幾個日誌級別分別表明什麼意思
Level | When it’s used |
---|---|
DEBUG |
Detailed information, typically of interest only when diagnosing problems. |
INFO |
Confirmation that things are working as expected. |
WARNING |
An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected. |
ERROR |
Due to a more serious problem, the software has not been able to perform some function. |
CRITICAL |
A serious error, indicating that the program itself may be unable to continue running. |
若是想把日誌寫到文件裏,也很簡單
import logging logging.basicConfig(filename='example.log', level=logging.DEBUG) logging.debug('This message should go to the log file') logging.info('So should this') logging.warning('And this, too') #example.log文件內容,level定義日誌級別,只要比debug高的就打印出來 DEBUG:root:This message should go to the log file INFO:root:So should this WARNING:root:And this, too
感受上面的日誌格式忘記加上時間啦,日誌不知道時間怎麼行呢,下面就來加上!
import logging logging.basicConfig(filename='example.log', format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.DEBUG) logging.debug('This message should go to the log file') logging.info('So should this') logging.warning('And this, too') #example.log文件裏的內容 02/24/2017 04:19:13 PM This message should go to the log file 02/24/2017 04:19:13 PM So should this 02/24/2017 04:19:13 PM And this, too
在日誌中顯示調用的文件名和函數,通常還要加上%(lineno)d
import logging logging.basicConfig(filename='example.log', format='%(asctime)s %(filename)s %(funcName)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.DEBUG) logging.debug('This message should go to the log file') logging.info('So should this') logging.warning('And this, too') def sayhi(): logging.info('call logging in sayhi') sayhi() #example.log文件裏的內容 02/24/2017 04:46:24 PM logging模塊.py <module> This message should go to the log file 02/24/2017 04:46:24 PM logging模塊.py <module> So should this 02/24/2017 04:46:24 PM logging模塊.py <module> And this, too 02/24/2017 04:46:24 PM logging模塊.py sayhi call logging in sayhi
日誌格式
%(name)s |
Logger的名字 |
%(levelno)s |
數字形式的日誌級別 |
%(levelname)s |
文本形式的日誌級別 |
%(pathname)s |
調用日誌輸出函數的模塊的完整路徑名,可能沒有 |
%(filename)s |
調用日誌輸出函數的模塊的文件名 |
%(module)s |
調用日誌輸出函數的模塊名 |
%(funcName)s |
調用日誌輸出函數的函數名 |
%(lineno)d |
調用日誌輸出函數的語句所在的代碼行 |
%(created)f |
當前時間,用UNIX標準的表示時間的浮 點數表示 |
%(relativeCreated)d |
輸出日誌信息時的,自Logger建立以 來的毫秒數 |
%(asctime)s |
字符串形式的當前時間。默認格式是 「2003-07-08 16:49:45,896」。逗號後面的是毫秒 |
%(thread)d |
線程ID。可能沒有 |
%(threadName)s |
線程名。可能沒有 |
%(process)d |
進程ID。可能沒有 |
%(message)s |
用戶輸出的消息 |
若是想同時把log打印在屏幕和文件日誌裏,就須要瞭解一點複雜的知識 了
Python 使用logging模塊記錄日誌涉及四個主要類,使用官方文檔中的歸納最爲合適:
logger提供了應用程序能夠直接使用的接口;
handler將(logger建立的)日誌記錄發送到合適的目的輸出;
filter提供了細度設備來決定輸出哪條日誌記錄;
formatter決定日誌記錄的最終輸出格式。
logger
每一個程序在輸出信息以前都要得到一個Logger。Logger一般對應了程序的模塊名,好比聊天工具的圖形界面模塊能夠這樣得到它的Logger:
LOG=logging.getLogger(」chat.gui」)
而核心模塊能夠這樣:
LOG=logging.getLogger(」chat.kernel」)
Logger.setLevel(lel):指定最低的日誌級別,低於lel的級別將被忽略。debug是最低的內置級別,critical爲最高
Logger.addFilter(filt)、Logger.removeFilter(filt):添加或刪除指定的filter
Logger.addHandler(hdlr)、Logger.removeHandler(hdlr):增長或刪除指定的handler
Logger.debug()、Logger.info()、Logger.warning()、Logger.error()、Logger.critical():能夠設置的日誌級別
handler
handler對象負責發送相關的信息到指定目的地。Python的日誌系統有多種Handler可使用。有些Handler能夠把信息輸出到控制檯,有些Logger能夠把信息輸出到文件,還有些 Handler能夠把信息發送到網絡上。若是以爲不夠用,還能夠編寫本身的Handler。能夠經過addHandler()方法添加多個多handler
Handler.setLevel(lel):指定被處理的信息級別,低於lel級別的信息將被忽略
Handler.setFormatter():給這個handler選擇一個格式
Handler.addFilter(filt)、Handler.removeFilter(filt):新增或刪除一個filter對象
每一個Logger能夠附加多個Handler。接下來咱們就來介紹一些經常使用的Handler:
1) logging.StreamHandler
使用這個Handler能夠向相似與sys.stdout或者sys.stderr的任何文件對象(file object)輸出信息。它的構造函數是:
StreamHandler([strm])
其中strm參數是一個文件對象。默認是sys.stderr
2) logging.FileHandler
和StreamHandler相似,用於向一個文件輸出日誌信息。不過FileHandler會幫你打開這個文件。它的構造函數是:
FileHandler(filename[,mode])
filename是文件名,必須指定一個文件名。
mode是文件的打開方式。參見Python內置函數open()的用法。默認是’a',即添加到文件末尾。
3) logging.handlers.RotatingFileHandler
這個Handler相似於上面的FileHandler,可是它能夠管理文件大小。當文件達到必定大小以後,它會自動將當前日誌文件更名,而後建立 一個新的同名日誌文件繼續輸出。好比日誌文件是chat.log。當chat.log達到指定的大小以後,RotatingFileHandler自動把 文件更名爲chat.log.1。不過,若是chat.log.1已經存在,會先把chat.log.1重命名爲chat.log.2。。。最後從新建立 chat.log,繼續輸出日誌信息。它的構造函數是:
RotatingFileHandler( filename[, mode[, maxBytes[, backupCount]]])
其中filename和mode兩個參數和FileHandler同樣。
maxBytes用於指定日誌文件的最大文件大小。若是maxBytes爲0,意味着日誌文件能夠無限大,這時上面描述的重命名過程就不會發生。
backupCount用於指定保留的備份文件的個數。好比,若是指定爲2,當上面描述的重命名過程發生時,原有的chat.log.2並不會被改名,而是被刪除。
4) logging.handlers.TimedRotatingFileHandler
這個Handler和RotatingFileHandler相似,不過,它沒有經過判斷文件大小來決定什麼時候從新建立日誌文件,而是間隔必定時間就 自動建立新的日誌文件。重命名的過程與RotatingFileHandler相似,不過新的文件不是附加數字,而是當前時間。它的構造函數是:
TimedRotatingFileHandler( filename [,when [,interval [,backupCount]]])
其中filename參數和backupCount參數和RotatingFileHandler具備相同的意義。
interval是時間間隔。
when參數是一個字符串。表示時間間隔的單位,不區分大小寫。它有如下取值:
S 秒
M 分
H 小時
D 天
W 每星期(interval==0時表明星期一)
midnight 天天凌晨
# -*- coding: UTF-8 -*- #blog:http://www.cnblogs.com/linux-chenyang/ import logging # create logger logger = logging.getLogger('TEST-LOG') logger.setLevel(logging.ERROR) #全局的日誌級別,好比message # create console handler and set level to debug ch = logging.StreamHandler() ch.setLevel(logging.INFO) #屏幕的日誌級別 # create file handler and set level to warning fh = logging.FileHandler("access.log") fh.setLevel(logging.WARNING) #文件的日誌級別 # create formatter formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') #日誌格式 # add formatter to ch and fh ch.setFormatter(formatter) fh.setFormatter(formatter) # add ch and fh to logger logger.addHandler(ch) logger.addHandler(fh) # 'application' code logger.debug('debug message') logger.info('info message') logger.warn('warn message') logger.error('error message') logger.critical('critical message') #屏幕輸出: 2017-02-24 19:19:24,027 - TEST-LOG - INFO - info message 2017-02-24 19:19:24,028 - TEST-LOG - WARNING - warn message 2017-02-24 19:19:24,029 - TEST-LOG - ERROR - error message 2017-02-24 19:19:24,030 - TEST-LOG - CRITICAL - critical message #文件輸出: 2017-02-24 19:19:24,028 - TEST-LOG - WARNING - warn message 2017-02-24 19:19:24,029 - TEST-LOG - ERROR - error message 2017-02-24 19:19:24,030 - TEST-LOG - CRITICAL - critical message #全局日誌級別>屏幕輸出&文件輸出
文件自動截斷例子
# -*- coding: UTF-8 -*- #blog:http://www.cnblogs.com/linux-chenyang/ import logging from logging import handlers # create logger logger = logging.getLogger('TEST-LOG') logger.setLevel(logging.DEBUG) #全局的日誌級別,好比message # create console handler and set level to debug ch = logging.StreamHandler() ch.setLevel(logging.INFO) #屏幕的日誌級別 # create file handler and set level to warning #fh = handlers.TimedRotatingFileHandler("access.log",when="S",interval=5,backupCount=3) fh = handlers.RotatingFileHandler("access.log",maxBytes=4,backupCount=3) fh.setLevel(logging.WARNING) #文件的日誌級別 # create formatter formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') #日誌格式 # add formatter to ch and fh ch.setFormatter(formatter) fh.setFormatter(formatter) # add ch and fh to logger logger.addHandler(ch) logger.addHandler(fh) # 'application' code logger.debug('debug message') logger.info('info message') logger.warn('warn message') logger.error('error message') logger.critical('critical message')