Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to use ">>" to append normal "stdout" output to a file.
D:\Python\python.exe D:\Code\Delete_Files_older_than_5_days.py > D:\code\Log\Delete_data_log.txt

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading