import os import argparse parser = argparse.ArgumentParser("LibEmpty", "Generates a list of the empty folders in a library.") def is_folder_empty(folder_path): num_files = len(os.listdir(folder_path)) return num_files == 0 def list_empty_folders(library_root): result = [] for folder in os.listdir(library_root): folder = os.path.join(library_root, folder) if os.path.isfile(folder): continue if is_folder_empty(folder): result.append(folder) return result def main(lib_dir, out_file): empty_folders = list_empty_folders(lib_dir) with open(out_file, 'w+') as writer: for folder in empty_folders: writer.write(folder+'\n') def check_args(lib, out): fatal_errors = False if not os.path.exists(lib): print('Error: The library root does not exist %s' % lib) fatal_errors = True if os.path.exists(out): print('The file %s will be overwritten.' % out) if fatal_errors: parser.print_help() print('Exiting...') exit(-1) if __name__ == '__main__': parser.add_argument('-l', '--lib_root', required=True, help='The root directory of the library.') parser.add_argument('-o', '--output', required=True, help='The path to the output file.') args = parser.parse_args() lib_root = args.lib_root out_path = args.output check_args(lib_root, out_path) main(lib_root, out_path)