Last active
July 2, 2018 06:41
-
-
Save mzfr/df4754bf8f6726c9b3b84647d132d8e4 to your computer and use it in GitHub Desktop.
Remove empty files and subdirectories within a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
requirements: | |
- tabulate | |
- argparse | |
""" | |
import os | |
import argparse | |
from tabulate import tabulate | |
def create_file_index(path: str): | |
"""Creates a list having multiple dictionaries in following format: | |
[{'name':<file_name>, 'path': '<path_to_file>'}] | |
:path: path for the directory | |
""" | |
file_index = [] | |
for dirs in os.walk(path): | |
for file_name in dirs[2]: | |
file_index.append({"path": dirs[0], "name": file_name}) | |
return file_index | |
def empty_files(path: str, ignore_files: list, remove=False): | |
"""Checks and remove empty files in a given directory | |
:path: Path of the directory | |
:ignore_files: a list of all the files to be ignored | |
:remove: optional vaue to remove the empty files | |
:return: Path of all the empty directories | |
""" | |
file_index = create_file_index(path) | |
headers = ["File-name", "Path"] | |
table = [] | |
for file in file_index: | |
path = os.path.join(file["path"], file["name"]) | |
if file["name"] in ignore_files: | |
continue | |
file_size = os.path.getsize(path) | |
if file_size == 0: | |
table.append([file["name"], path]) | |
if remove: | |
for removed in table: | |
os.remove(removed[1]) | |
print("Removed files are: ") | |
else: | |
print("Empty files are: ") | |
print(tabulate(table, headers, tablefmt="fancy_grid")) | |
def empty_dir(path: str, ignore_dir: list, remove=False): | |
"""Look for empty subdirectories in a directory and remove them | |
:path: Path of the directory | |
:ignore_dir: A list of all the directories to be ignored | |
:remove: optional value to remove the empty subdirectories | |
:return: path of all the empty directories | |
""" | |
headers = ["name", "Path"] | |
table = [] | |
for dirpath, dirs, files in os.walk(path): | |
dirs[:] = [d for d in dirs if d not in ignore_dir] | |
if not dirs and not files: | |
name = os.path.basename(dirpath) | |
table.append([name, dirpath]) | |
if remove: | |
for removed in table: | |
os.rmdir(removed[1]) | |
print("removed directories are: ") | |
else: | |
print("Empty directories are: ") | |
print(tabulate(table, headers, tablefmt="fancy_grid")) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--dir", help="path of the directory to check", default=os.getcwd()) | |
parser.add_argument("--ignore-file", help="names of the file to be ignored", nargs="+") | |
parser.add_argument("--ignore-dir", help="name of the directory to be ignored", nargs="+") | |
parser.add_argument("--remove", help="delete the empty folders and files", action="store_true") | |
args = parser.parse_args() | |
ignored_files = ["__init__.py"] | |
ignore_dir = [".git"] | |
if args.ignore_file: | |
ignored_files.extend(args.ignore_file) | |
if args.ignore_dir: | |
ignore_dir.extend(args.ignore_dir) | |
empty_files(args.dir, ignored_files, args.remove) | |
empty_dir(args.dir, ignore_dir, args.remove) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment