日誌是一種追蹤軟件運行時所發生事件的方法linux
一、簡單的日誌編寫(文件輸出)windows
import logging # 日誌模塊 # 日誌配置,把日誌內容存到文件中 logging.basicConfig(level=logging.ERROR, # 級別修改,注意必定是大寫 format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:S', # 指定時間格式 filename='test.log', # 日誌內容存到該文件目錄下 filemode='w') # 模式,每次開始都刷新重寫,若是想要以前的內容就把‘w’改成‘a’ # 日誌級別,()裏面是日誌的內容,能夠隨意添加 logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')
輸出:Tue, 05 Mar 2019 16:37:S logging .py[line:16] ERROR error message
Tue, 05 Mar 2019 16:37:S logging .py[line:17] CRITICAL critical message二、文件和屏幕同時輸出spa
import logging logger = logging.getLogger() # 建立一個hangler, 用於寫入日誌文件 fh = logging.FileHandler('test.log') # 文件輸出對象 # 再建立一個handler, 用於輸出到控制檯 ch = logging.StreamHandler() # 屏幕輸出對象 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) # 文件輸出建立一個formatter輸出流格式 ch.setFormatter(formatter) logger.addHandler(fh) # logger裏面有兩種輸出對象,文件和屏幕 logger.addHandler(ch) logger.setLevel(logging.WARNING) # 修改輸出日誌的級別,從DEBUG開始,輸出5行,注意全是大寫
# 日誌等級是從上到下依次升高的,即:DEBUG < INFO < WARNING < ERROR < CRITICAL logging.debug('debug message') logging.info('info message') logging.warning('waring message') logging.error('error message') logging.critical('critical message')
做用:對大量的文件和文件路徑進行操做日誌
import os print(os.getcwd()) # 獲取當前工做目錄 os.chdir(r'F:\My test\fullstack\week2') # 改變當前工做目錄,加r表示原生字符串,不進行任何轉義 print(os.curdir) # 獲取當前目錄:‘.’ 用一個‘.’ 表示 print(os.pardir) # 獲取當前目錄的父級目錄字符串名:‘..’ os.makedirs('abc') # 建立文件夾,默認建立在當前目錄下,若是文件已存在,報錯 os.makedirs('abc\\long') # 建立多層目錄,用 \\ 隔開 os.mkdir('abc') # 只能生成一個文件 os.mkdir('abc\\long') # 在已經有abc 文件的基礎上,再生成一個文件loing os.removedirs('abc\\long') # 刪除文件夾(只能刪除空文件)先判斷long中有沒有文件,沒有就刪除,一層層往外找 os.rmdir('abc') # 刪除單級空目錄,若目錄不爲空沒法刪除 dirs = os.listdir(r'F:\My test\fullstack\week2') # 打印絕對目錄裏面的全部文件 print(dirs) os.remove('abc\\dff') # 刪除dff文件,只能刪除文件不能刪除文件夾, os.rename('abc', 'www') # 修改文件(夾)名, abc改成www info = os.stat('.\\www') # 獲取文件的詳細信息 print(info) print(info.st_size) # 獲取文件大小 print(os.sep) # \ 獲取當前系統的路徑分割符 q = os.sep # 用q替換分隔符, q是一個變量 # windows裏換行符爲: \r\n linux裏換行符爲:\n print(os.pathsep) # 輸出用於路徑分割文件路徑的字符 print(os.name) # 輸出字符串當前使用平臺 win->'nt' print(os.system("dir")) # 顯示當前目錄的內容 print(os.environ) # 獲取系統環境變量 print(os.path.abspath('./www')) # 獲取文件www的絕對路徑 s = os.path.split(r'F:\My test\fullstack\week2\day3\www') print(s) # 把文件路徑和文件名分隔開 s = os.path.dirname('F:\\My test\\fullstack\\week2\\day3') print(s) # 返回當前文件上一級的文件夾路徑 s = os.path.basename('F:\\My test\\fullstack\\week2\\day3') print(s) # day3 返回文件名,就是 os.path.split(path) 的第二個內容 s = os.path.exists('F:\\My test\\fullstack\\week2\\day3') print(s) # path存在返回True,不存在返回False s = os.path.isabs('F:\\My test\\fullstack\\week2\\day3') print(s) # 若是path是絕對路徑返回True,不然返回False s = os.path.isfile('F:\\My test\\fullstack\\week2\\day3') print(s) # 若是path是一個存在的文件返回True,不然返回False s = os.path.isdir('F:\\My test\\fullstack\\week2\\day3') print(s) # 若是path是一個存在的目錄返回True,不然返回False s = os.path.join('F:\\My test\\fullstack\\week2\\day3', 'F:\\My test\\fullstack\\week2\\day4') print(s) # 路徑拼接: F:\My test\fullstack\week2\day4 s = os.path.getatime('F:\\My test\\fullstack\\week2\\day3') print(s) # 返回path所指向文件或目錄的最後存取時間 s = os.path.getmtime('F:\\My test\\fullstack\\week2\\day3') print(s) # 返回path所指向文件或目錄的最後修改時間
做用:用於生成和修改常見的配置文件,經過字典錄入
import configparser # 全部操做都圍繞config對象進行 config = configparser.ConfigParser() # 寫入文件 config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'} # 生成的是字典的形式:DEFAULG 是鍵,中括號裏面是鍵值,下同 config['bitbucket.org'] = {'User': 'hg'} config['topsecret.server.com'] = {} topsecret = config['topsecret.server.com'] topsecret['Host Port'] = '50022' topsecret['ForwardXll'] = 'no' config['DEFAULT']['ForwardXll'] = 'yes' with open('example.ini', 'w') as configfile: config.write(configfile)
輸出:code
寫入的文件還能夠讀出來:orm
import configparser config = configparser.ConfigParser() # 全部操做都圍繞config對象進行 config.read('example.ini') # 讀配置文件操做 print(config.sections()) # 不能讀取默認塊兒的內容:['bitbucket.org', 'topsecret.server.com'] print(config.defaults()) # 讀取默認塊兒DEFAULT裏面的信息 print(config['bitbucket.org']['User']) # hg for key in config: print(key) # 打印全部塊兒的名字 for key in config['bitbucket.org']: print(key) # DEFAULT 是特殊的,打印別的塊下的內容它也跟在下面
配置文件的增刪改:server
config.remove_section('topsecret.server.com') # 刪除塊:topsecret.server.com config.write(open('i.cfg', 'w')) # 不管如何修改都要加這句,下同 print(config.has_section('topsecret.server.com')) # 判斷有沒有該字符串:False (由於前面刪除了) config.set('bitbucket.org', 'user', 'xiaoss') # 修改操做:('塊', '鍵', '要修改的值') config.remove_option('DEFAULT', 'forwardxll') # 刪除塊下的某個鍵值對兒:('塊', '鍵值') config.write(open('example.ini', 'w')) # 注意對文件example.ini,是徹底清空從新寫入,是覆蓋,文件名與原來相同
注意:一個文件一旦命名絕對不會被修改,只能從新創建文件寫入,修改並非真正的修改,而是創建新的文件放在對象裏面而後覆
蓋原來的對象對象