From df7d456c301d0fb54a342e196cdb4c7963a6a10d Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sat, 15 Apr 2017 21:46:43 +0300 Subject: [PATCH] Add remove-old-files.py - the main script in the package --- remove-old-files.py | 31 +++++++++++++++++++++++++++++++ remove-old-files/__init__.py | 1 - remove-old-files/__version__.py | 1 - setup.py | 2 +- 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100755 remove-old-files.py delete mode 100644 remove-old-files/__init__.py delete mode 100644 remove-old-files/__version__.py diff --git a/remove-old-files.py b/remove-old-files.py new file mode 100755 index 0000000..79d5b69 --- /dev/null +++ b/remove-old-files.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python + +from __future__ import print_function +import argparse +from datetime import datetime, timedelta +import os + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Remove old files') + 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') + args = parser.parse_args() + + count = errors = size = 0 + now = datetime.now() + + for dirpath, dirnames, filenames in os.walk(args.start_dir, topdown=False): + for fname in filenames: + file_path = os.path.join(dirpath, fname) + file_modified = datetime.fromtimestamp(os.path.getmtime(file_path)) + if file_modified + timedelta(days=args.older) < now: + count += 1 + size += os.path.getsize(file_path) + try: + os.remove(file_path) + except OSError: + errors += 1 + + print("Removed {0:d} files, freed {1:d} bytes, {2:d} errors".format( + count, size, errors)) diff --git a/remove-old-files/__init__.py b/remove-old-files/__init__.py deleted file mode 100644 index 792d600..0000000 --- a/remove-old-files/__init__.py +++ /dev/null @@ -1 +0,0 @@ -# diff --git a/remove-old-files/__version__.py b/remove-old-files/__version__.py deleted file mode 100644 index b8023d8..0000000 --- a/remove-old-files/__version__.py +++ /dev/null @@ -1 +0,0 @@ -__version__ = '0.0.1' diff --git a/setup.py b/setup.py index c789abe..20ab4a3 100755 --- a/setup.py +++ b/setup.py @@ -33,6 +33,6 @@ setup(name='remove-old-files', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', ], - scripts=[], + scripts=['remove-old-files.py'], requires=[], ) -- 2.39.5