Add options to limit files or to exclude files by name, extension, path.
-
-
-Remove empty directories.
Options::
+ -e, --empty-dirs
+ remove empty directories
-o days, --older days
remove files older than this number of days;
this is a required option
News
====
+0.1.0 (2017-04-16)
+------------------
+
+* Remove empty directories.
+
0.0.1 (2017-04-16)
------------------
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')
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))
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))
assert os.system("remove-old-files.py --older 100 .") == 0
assert_files_exist(test3)
assert_files_not_exist(test4)
+
+
+def test_remove_empty_directory():
+ create_files(['test3', 'test4'], 'subdir')
+ test3 = os.path.join('subdir', 'test3')
+ test4 = os.path.join('subdir', 'test4')
+ assert_files_exist([test3, test4])
+ os.utime(test3, (0, 0))
+ os.utime(test4, (0, 0))
+ assert os.system("remove-old-files.py --older 100 .") == 0
+ assert_files_exist('subdir')
+ assert_files_not_exist([test3, test4])
+ assert os.system("remove-old-files.py -e --older 100 .") == 0
+ assert_files_not_exist('subdir')