'How to delete older files and keep last day files for each month in python

I need to retain the backup file started on 31st april and ended next day May 1. backup timings differ for each folder, but the backup files are identical



Solution 1:[1]

this is probably a way to solve your problem : os.path.getctime getting the date of creation of the file and os.path.getmtime getting the date of last modification of the file

import os

if os.path.getctime("test.txt") > os.path.getctime("test2.txt"):
    print("test.txt is more recent")
    os.remove("test2.txt")
else:
    print("test2.txt is more recent")
    os.remove("test.txt")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kal-1