X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=remove-old-files.py;h=5ff7d80402730403944807876f8de624fcf45fc1;hb=9e09c7ca4012167f674e50fc834d6c10de2431d2;hp=79d5b69ca1986ac2674d75bbc9987020a8c4adab;hpb=f753377d712c7af64ae840d82923d566b2c7bc8c;p=ppu.git diff --git a/remove-old-files.py b/remove-old-files.py index 79d5b69..5ff7d80 100755 --- a/remove-old-files.py +++ b/remove-old-files.py @@ -7,6 +7,8 @@ import os if __name__ == '__main__': parser = argparse.ArgumentParser(description='Remove old files') + parser.add_argument('-e', '--empty-dirs', action='store_true', + help='remove empty directories') parser.add_argument('-o', '--older', required=True, type=int, help='remove files older than this (in days)') parser.add_argument('start_dir', help='start from this directory') @@ -16,6 +18,7 @@ if __name__ == '__main__': now = datetime.now() for dirpath, dirnames, filenames in os.walk(args.start_dir, topdown=False): + has_newer_files = False for fname in filenames: file_path = os.path.join(dirpath, fname) file_modified = datetime.fromtimestamp(os.path.getmtime(file_path)) @@ -26,6 +29,14 @@ if __name__ == '__main__': os.remove(file_path) except OSError: errors += 1 + else: + has_newer_files = True + if args.empty_dirs and not dirnames and not has_newer_files: + count += 1 + try: + os.rmdir(dirpath) + except OSError: + errors += 1 - print("Removed {0:d} files, freed {1:d} bytes, {2:d} errors".format( + print("Removed {0:d} files/dirs, freed {1:d} bytes, {2:d} errors".format( count, size, errors))