Hello I have written python code which run by task scheduler. This code delete the folder which are older than 5 days. code is working fine but I took output of this code in log text file. whenever code run existing log file overwritten. I want see every schedule log , that’s not possible now.
How I can get output of code in different file every time or in same file without overwriting data.
import os
import sys
import datetime
import shutil
path=r"C:UsersIliyasOneDriveDocuments"
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
This code run by .bat file in task scheduler
D:Pythonpython.exe D:CodeDelete_Files_older_than_5_days.py > D:codeLogDelete_data_log.txt
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You need to use >> to append stdout to a file.
D:Pythonpython.exe D:CodeDelete_Files_older_than_5_days.py >> D:codeLogDelete_data_log.txt
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0