python大文件排序_python实现按创建时间对文件排序
測試中,測試log是經常需要保存一段時間以便于后續查詢,但是如果一段時間不刪除,會導致硬盤空間變小而影響自動化測試,通常空間太小,自動化測試case就不能調用了,或者即使調用,可能會引起新測試的log無法保存。
這里提供一個python腳本,用于對log按創建時間進行排序。因為通常,我們刪log也是先刪除相對較老的log。排完序以后,就可以對log進行刪除操作。可以設定一些門限,比如文件大小,當存放log的文件超過一定大小時就啟動刪除,每刪除一個log判斷一次文件大小是否小于設定門限,直到小于門限。這樣就能控制log存放文件夾的大小并保證每次刪除都是先刪掉老的log。
代碼:
import os
import os.path
def swap(target_list): #sorting folder list according to create time
for i in range(len(target_list)):
if i < (len(target_list)-1) and target_list[i][0] > target_list[i+1][0]:
temp = target_list[i+1]
target_list[i+1] = target_list[i]
target_list[i] = temp
else:
continue
return target_list
if __name__ == '__main__':
dir_log = "D:"
files_with_time = [(os.path.getctime(dir_log + "/" + folder),dir_log + "/" + folder) for folder in os.listdir(dir_log)]
#create a list with 2 elements, one is folder name and the other is create time
print "the orginal filelist is"
print files_with_time
cyc_times = len(files_with_time)
i = 0
while(i < cyc_times): # a cycle to re-arrange folder
i = i + 1
files_with_time = swap(files_with_time)
print "the seq list is"
print files_with_time
總結
以上是生活随笔為你收集整理的python大文件排序_python实现按创建时间对文件排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑开机报警声音大全详解及处理
- 下一篇: wordcloud python3.6能