一、shutil:檔案目錄相關的模塊
-
拷貝檔案:copy2
import shutil shutil.copy2('原檔案', '現檔案')
-
拷貝目錄:copytree
import shutil # 拷貝目錄并且不要原目錄里面所有py結尾和txt結尾的檔案(默認可以不寫) shutil.copytree('原目錄', '新目錄', ignore=shutil.ignore_patterns('*.py', '*.txt'))
-
洗掉目錄:rmtree
import shutil # 洗掉目錄,且不管這個目錄是否在使用中,強制洗掉,(默認為False不強制洗掉) shutil.rmtree('目錄', ignore_errors=True)
-
移動檔案或目錄:move
import shutil # 也可移動并改名(默認在相對目錄下) shutil.move('檔案', '新目錄\新檔案.txt')
-
獲取磁盤的使用空間:disk_usage
import shutil # '.'點為當前磁盤,可以改成'D\\'盤 total, used, free = shutil.disk_usage('.') # 因為獲取到的是kb,所以要除以1024的3次方還能獲取到GB print(f'當前磁盤工:{int(total/1073741824)}GB,已使用:{int(used/1073741824)}GB,剩余:{int(free/1073741824)}GB') # 輸出 當前磁盤工:100GB,已使用:65GB,剩余:34GB
-
壓縮檔案:make_archive
impoirt shutil shutil.make_archive('被壓縮檔案夾的名字', 'zip', '壓縮后檔案夾路徑')
-
解壓檔案:unpack_archive
impoirt shutil shutil.unpack_archive('zip檔案的路徑.zip','解壓到目的檔案夾路徑')
二、logging:日志模塊
-
為什么要寫log?
-
用來記錄用戶的行為——>資料分析
-
用來記錄用戶的行為——>操作審計
-
排查代碼中的錯誤
-
-
輸出內容是有等級的 : 默認處理warning級別以上的所有資訊
import logging # 在寫程式的時候要寫,但是正常運行的程序中不執行,出現問題在重啟打開除錯模式,復現報錯看日志 logging.debug('debug 資訊') # 除錯——默認不顯示 logging.info('info 資訊') # 資訊——默認不顯示 logging.warning('warning 資訊') # 警告 logging.error('error 資訊') # 錯誤 logging.critical('critical 資訊') # 批判性(嚴重)的 # 輸出 WARNING:root:warning 資訊 ERROR:root:error 資訊 CRITICAL:root:critical 資訊
-
輸出到螢屏
import logging logging.basicConfig( # format是設定輸出格式 format='%(asctime)s - %(levelname)s[第%(lineno)d行]-%(module)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S', # 設定時間格式 level=logging.DEBUG # 設定日志級別,最低顯示資訊從debug開始(默認從warning) ) logging.debug('debug 資訊錯誤 ') logging.info('info 資訊錯誤 ') logging.warning('warning 錯誤資訊') logging.error('error 錯誤資訊') logging.critical('critical 錯誤資訊') # 輸出 2021-04-27 21:29:06 - DEBUG[第78行]-6.logging模塊: debug 錯誤資訊 2021-04-27 21:29:06 - INFO[第79行]-6.logging模塊: info 錯誤資訊 2021-04-27 21:29:06 - WARNING[第80行]-6.logging模塊: warning 錯誤資訊 2021-04-27 21:29:06 - ERROR[第81行]-6.logging模塊: error 錯誤資訊 2021-04-27 21:29:06 - CRITICAL[第82行]-6.logging模塊: critical 錯誤資訊
-
輸出到檔案
import logging ? fh = logging.FileHandler('tmp.log', encoding='utf-8') # 創建一個檔案處理程式 log_file = open('log.log', encoding='utf-8', mode='a') # 日志檔案句柄 logging.basicConfig( format='%(asctime)s - %(levelname)s[第%(lineno)d行]-%(module)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S %p', stream=log_file, # 輸出到檔案句柄, handlers=[fh, ] # 或者用這個方法將檔案處理程式添加進去(只能選一個) level=logging.DEBUG ) logging.debug('debug 資訊錯誤 ') logging.info('warning 資訊錯誤 ') logging.warning('warning 資訊錯誤 ') logging.error('error 資訊錯誤 ') logging.critical('critical 資訊錯誤 ')
-
日志切割
import time import logging from logging import handlers ? sh = logging.StreamHandler() # 在控制臺能看見報錯資訊 # 按照大小做切割,滿了1024kb后就切割下一個檔案,最多出現5個檔案,超過之后會把最后的那個檔案洗掉 rh = handlers.RotatingFileHandler('myapp.log', maxBytes=1024, backupCount=5) # 按照時間做切割,每個5秒切割一個檔案 fh = handlers.TimedRotatingFileHandler(filename='time.log', when='s', interval=5, encoding='utf-8') logging.basicConfig( format='%(asctime)s - %(levelname)s[第%(lineno)d行]-%(module)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S %p', level=logging.DEBUG, handlers=[fh, rh, sh] # 將每個設定放入handlers中 ) for i in range(1, 100000): time.sleep(1) logging.error('錯誤資訊 error %s' % str(i)) # 輸出 2021-04-27 22:40:35 PM - ERROR[第177行]-6.logging模塊: 錯誤資訊 error 1 2021-04-27 22:40:36 PM - ERROR[第177行]-6.logging模塊: 錯誤資訊 error 2 2021-04-27 22:40:37 PM - ERROR[第177行]-6.logging模塊: 錯誤資訊 error 3 .... ....
-
logging.basicConfig:配置引數
logging.basicConfig()函式中可通過具體引數來更改logging模塊默認行為,可用引數有: ? filename:——用指定的檔案名創建FiledHandler,這樣日志會被存盤在指定的檔案中, filemode:——檔案打開方式,在指定了filename時使用這個引數,默認值為“a”還可指定為“w”, format:——指定handler使用的日志顯示格式, datefmt:——指定日期時間格式, level:——設定rootlogger的日志級別 stream:——用指定的stream創建StreamHandler,可以指定輸出到sys.stderr,sys.stdout或者檔案(f=open(‘test.log’,’w’)),默認為sys.stderr,若同時列出了filename和stream兩個引數,則stream引數會被忽略, ? format引數中可能用到的格式化串: %(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 ——用戶輸出的訊息
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/282221.html
標籤:其他
