Home > database >  How to get python Script output in Text file ,without overwriting current one text file
How to get python Script output in Text file ,without overwriting current one text file

Time:04-30

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:\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

This code run by .bat file in task scheduler

D:\Python\python.exe D:\Code\Delete_Files_older_than_5_days.py > D:\code\Log\Delete_data_log.txt

CodePudding user response:

You need to use >> to append stdout to a file.

D:\Python\python.exe D:\Code\Delete_Files_older_than_5_days.py >> D:\code\Log\Delete_data_log.txt
  • Related