![](/img/trans.png)
[英]Including date in the log file name & creating log files of certain size while keeping last 3 log files in python with `logging.config.dictConfig()`
[英]Replacing date in log files with file name with python
根據您的所有評論,我進行了如下修改
For example:
Let say there are 3 log files in a folder, which are 20190626.txt, 20190625.txt, 20190624.txt
The log format like below and let says all the content in the txts are the same:
2019-06-26 server2 rcd[308]: Loaded 12 packages in 'ximian-red-carpet' 20190626
2019-06-26 server2 rcd[308]: id=304 COMPLETE Download 20190626
2019-06-26 server2 rcd[308]: Unable to downloaded licenses info 20190626
2019-06-26 server2 /USR/SBIN/CRON[6808]: (root) CMD ( /usr/lib/sa/sa1 ) 20190626
2019-06-26 server2 /USR/SBIN/CRON[6837]: (root) CMD ( /usr/lib/sa/sa1 ) 20190626
And I want to replace "2019-06-26" according to the file name which are "20190625", "20190624", but replaced in "2019-06-25", 2019-06-24" and so on.
Then in 20190624.txt, the content will become:
2019-06-24 server2 rcd[308]: Loaded 12 packages in 'ximian-red-carpet' 20190624
2019-06-24 server2 rcd[308]: id=304 COMPLETE Download 20190624
2019-06-24 server2 rcd[308]: Unable to downloaded licenses info 20190624
2019-06-24 server2 /USR/SBIN/CRON[6808]: (root) CMD ( /usr/lib/sa/sa1 ) 20190624
2019-06-24 server2 /USR/SBIN/CRON[6837]: (root) CMD ( /usr/lib/sa/sa1 ) 20190624
並以相同的方式處理文件中的其余日志
目前,我只能想到的邏輯是
對於代碼,我嘗試如下:
#!/usr/bin/python
import re
import os
import sys
import string
mylist = os.listdir('C:/Users/xxx')
length = len(mylist)
olddate = '2019-06-26'
for i in range (0, length - 1):
newfilename = mylist[i]
with open(newfilename) as f:
newdate = newfilename <<<< This is wrong, because newfilename is "2019062X.txt", but I want it to be "20xx-xx-xx" according to the file name
rtext=f.read().replace(olddate, newdate)
with open(newfilename,"w") as f:
f.write(rtext)
謝謝你的幫助!!
您可以在python re模塊中使用正則表達式。
為了使用正則表達式替換文本,請使用re.sub函數:
import re
'''old file data'''
oldfilename = "20480824.txt"
oldtext = " \nblabla foo 2048-08-24 this \n\
2048-08-24 foo bar \n\
2048-08-24: Socket created..."
'''new file data'''
newfilename = "20481023.txt"
'''compute new file data'''
olddate = re.sub(r'(\d{4})(\d{2})(\d{2})\.txt', '\g<1>-\g<2>-\g<3>', oldfilename)
newdate = re.sub(r'(\d{4})(\d{2})(\d{2})\.txt', '\g<1>-\g<2>-\g<3>', newfilename)
newtext = re.sub(r'{}'.format(olddate), '{}'.format(newdate), oldtext)
print("---olddate : " + olddate)
print("---newdate : " + newdate)
print("---oldtext : " + oldtext)
print("---newtext : " + newtext)
輸出為:
---olddate : 2048-08-24
---newdate : 2048-10-23
---oldtext :
blabla foo 2048-08-24 this
2048-08-24 foo bar
2048-08-24: Socket created...
---newtext :
blabla foo 2048-10-23 this
2048-10-23 foo bar
2048-10-23: Socket created...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.