Python清理過期檔案

2022-02-06 08:04:01 字數 1181 閱讀 4092

改程式執行後,會清理 test/資料夾中距離現在超過一天的以 .xml 結尾的檔案

#

coding: utf-8

import

time

import

osroot = os.path.dirname(__file__

)def

clear_file(root_dir):

print(333333)

for root, dir, names in

os.walk(root_dir):

print

(root, dir, names)

for name in

names:

filename =os.path.join(root, name)

ct =os.stat(filename).st_ctime

nt =time.time()

s1 =time.localtime(ct)

s2 =time.localtime(nt)

print

"s%檔案建立時間為%s

", (name, time.strftime('

%y-%m-%d

', s1))

print

"當前時間為:

", time.strftime('

%y-%m-%d

', s2)

tt = nt -ct

m, s = divmod(tt, 60)

h, m = divmod(m, 60)

d, h = divmod(h, 24)

print

"檔案建立距離現在已%02d天%02d時%02d分%02d秒

" %(d, h, m, s)

if d >= 1 and name.endswith('

.xml'):

print

"檔案超過一天,執行清理

"os.remove(filename)

elif d < 1 and m < 10 and name.endswith('

.xml'):

os.remove(filename)

if __name__ == '__main__':

root = root+'

/test/

'clear_file(root)

python定期清理過期資料

功能 定期清理資料夾中的過期資料 usr bin python3 coding utf 8 import os import time import datetime import shutil import socket import logging import sys 獲取主機名 hostna...

linux 清理過期日誌

linux是乙個很能自動產生檔案的系統,日誌 郵件 備份等。雖然現在硬碟廉價,我們可以有很多硬碟空間供這些檔案浪費,讓系統定時清理一些不需要的檔案很有一種爽快的事情。不用你去每天惦記著是否需要清理日誌,不用每天收到硬碟空間不足的報警簡訊,想好好休息的話,讓我們把這個事情交給機器定時 去執行吧。1.刪...

nginx日誌切割並清理過期日誌

1 在nginx目錄下新建乙個指令碼檔案cut nginx logs.sh,具體nginx路徑根據實際情況更改,指令碼內容如下 bin bash logs path為日誌存放路徑 logs path home admin mid nginx logs yesterday date d yesterd...