您好,我已經撰寫了由任務調度程式運行的 python 代碼。此代碼洗掉超過 5 天的檔案夾。代碼作業正常,但我在日志文本檔案中輸出了這段代碼。每當代碼運行覆寫現有日志檔案時。我想查看每個計劃日志,現在不可能。如何在不覆寫資料的情況下每次或在同一檔案中獲得不同檔案中的代碼輸出。
import os
import sys
import datetime
import shutil
path=r"C:\Users\Iliyas\OneDrive\Documents"
all_dir=os.listdir(path)
age=5
today=datetime.datetime.now()
for each_dir in all_dir:
each_dir_path=os.path.join(path,each_dir)
if os.path.isdir(each_dir_path):
die_cre_date=datetime.datetime.fromtimestamp(os.path.getctime(each_dir_path))
dif_days=(today-die_cre_date).days
if dif_days > age:
print(f' The {each_dir_path} folder in older the {dif_days} days, deleting it:')
shutil.rmtree(each_dir_path)
print("path deleted")
else:
print("there is nothing delete older than 5 days")
break
此代碼由任務調度程式中的 .bat 檔案運行
D:\Python\python.exe D:\Code\Delete_Files_older_than_5_days.py > D:\code\Log\Delete_data_log.txt
uj5u.com熱心網友回復:
您需要使用“>>”將正常的“stdout”輸出附加到檔案中。
D:\Python\python.exe D:\Code\Delete_Files_older_than_5_days.py > D:\code\Log\Delete_data_log.txt
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/468375.html
