From 9e09c7ca4012167f674e50fc834d6c10de2431d2 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 16 Apr 2017 10:46:55 +0300 Subject: [PATCH] Remove empty directories --- TODO | 3 --- docs/index.rst | 2 ++ docs/news.rst | 5 +++++ remove-old-files.py | 13 ++++++++++++- tests/test_remove_old_files.py | 14 ++++++++++++++ 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/TODO b/TODO index 459559d..0cd98e8 100644 --- a/TODO +++ b/TODO @@ -2,6 +2,3 @@ Add option -v/--verbose to report every removed file. Add options to limit files or to exclude files by name, extension, path. - - -Remove empty directories. diff --git a/docs/index.rst b/docs/index.rst index 1fa4162..a438400 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -29,6 +29,8 @@ Usage:: Options:: + -e, --empty-dirs + remove empty directories -o days, --older days remove files older than this number of days; this is a required option diff --git a/docs/news.rst b/docs/news.rst index 9dc7a82..3d3375d 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -1,6 +1,11 @@ News ==== +0.1.0 (2017-04-16) +------------------ + +* Remove empty directories. + 0.0.1 (2017-04-16) ------------------ 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)) diff --git a/tests/test_remove_old_files.py b/tests/test_remove_old_files.py index e50b6fa..67da601 100755 --- a/tests/test_remove_old_files.py +++ b/tests/test_remove_old_files.py @@ -61,3 +61,17 @@ def test_recursive(): 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') -- 2.39.2