python對執行程式進行日誌檔案記錄

2021-07-04 02:46:57 字數 1089 閱讀 5261

import logging  

import psutil

import os

#設定乙個日誌輸出檔案

log_filename="logging.txt"

#設定日誌輸出格式

log_format=' [%(asctime)s] %(message)s'

i=0#獲取當前執行的pid

p1=psutil.process(os.getpid())

#將日誌檔案格式化

logging.basicconfig (format=log_format,datafmt='%y-%m-%d %h:%m:%s %p',level=logging.debug,filename=log_filename,filemode='w')

#cpu使用率

cpu_persent='cpu使用率:' +(str)(p1.cpu_percent(1))

#mem_persent='記憶體使用率:'+ (str)(p1.memory_percent)

while i<5:

logging.debug('出紙檢測 '+cpu_persent)

i=i+1

print i

想關引數介紹:

#這條是定義日誌格式的乙個變數。顯示的條目可以是以下內容:

%(levelname):日誌級別的名字格式

%(levelno)s:日誌級別的數字表示

%(name)s:日誌名字

%(funcname)s:函式名字

%(asctime):日誌時間,可以使用datefmt去定義時間格式,如上圖。

%(pathname):指令碼的絕對路徑

%(filename):指令碼的名字

%(module):模組的名字

%(thread):thread id

%(threadname):執行緒的名字

日誌的設定都是使用basicconfig()方法,需要注意的是,日誌寫入檔案的預設方式是『a』,也就是追加,如果想覆蓋檔案,則使用如上圖那樣,使用filemode='w'。

python 執行可執行程式

python do exe.pyw coding utf 8 import os exe dir c program files q dir exe file q dir.exe def do cmd dir,file if os.access dir,os.f ok os.chdir dir if...

Python 如何執行程式

1.python 直譯器 直譯器是一種讓其他程式執行起來的程式。當你寫一段 python 程式,python 直譯器將讀取程式,並按照其中的命令執行,得出結果。實際上,直譯器是 與機器的計算機硬體這間的軟體邏輯層。2.python 位元組碼 python內部 對大多數使用者是完全隱藏的 會先將源 編...

linux自動執行程式

linux 的啟動指令碼位於 etc init.d rcs 注rcs中 s是大寫字母 vi etc init.d rcs 進入vi後,按i 編輯.移動上下左右鍵到,你想插入的位置,然後打字。這時跟記事本是一樣的。打完字後,按esc 然後輸入 wq 注,第乙個字元是冒號 也要輸入 就儲存退出了。若想自...