Skip to content

Instantly share code, notes, and snippets.

@kamui-fin
Created June 3, 2020 00:02
Show Gist options
  • Save kamui-fin/7bc0640f1db408ab4e2c8ef2ecbd82cc to your computer and use it in GitHub Desktop.
Save kamui-fin/7bc0640f1db408ab4e2c8ef2ecbd82cc to your computer and use it in GitHub Desktop.
Script to clean up any folder
from pathlib import Path
from pprint import pprint
from shutil import copyfile
import os
import argparse
import tqdm
import json
"""
usage: main.py [-h] [--outputinfo] [--type TYPE] source destination
script to clean up any folder
positional arguments:
source The folder to clean up
destination The output cleaned up folder
optional arguments:
-h, --help show this help message and exit
--outputinfo Just output a json file with folder details
--type TYPE Only make a folder for [type1,type2,type2]
Here is a command you can run to extract all the books from your PC
python3.8 main.py test books --type pdf,epub,azw,mobi,chm,djvu,html,htm --outputinfo
"""
def organize_files(directory):
data_dir = Path(directory)
all_files = data_dir.rglob("*.*")
bins = {}
pbar = tqdm.tqdm(list(all_files))
pbar.set_description("Parsing files...")
for file in pbar:
ext = file.suffix[1:]
if ext in bins:
bins[ext].append(file)
else:
bins[ext] = [file]
return bins
def make_organized_folder(output_folder, bins, type):
try:
os.mkdir(output_folder)
except FileExistsError as e:
print(f"You already have a folder named {output_folder}!")
return 1
pbar = tqdm.tqdm(bins.items())
pbar.set_description("Tidying up files...")
for ext, files in pbar:
if type:
if ext not in type:
continue
if not os.path.exists(f"{output_folder}/{ext}"):
os.mkdir(f"{output_folder}/{ext}")
for file in files:
copyfile(file, f"{output_folder}/{ext}/{Path(file).name}")
return 0
def output_info(info, types):
for k, v in info.items():
for num, file in enumerate(v):
info[k][num] = str(file)
with open("cleaned_info.json", encoding="utf-8", mode="w") as f:
json.dump({k: v for k, v in info.items() if k in types}, f)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="script to clean up any folder")
parser.add_argument("source", help="The folder to clean up")
parser.add_argument("destination", help="The output cleaned up folder")
parser.add_argument(
"--outputinfo", action="store_true", help="Just output a json file with folder details")
parser.add_argument("--type", type=str,
help="Only make a folder for [type1,type2,type2]")
args = parser.parse_args()
bins = organize_files(args.source)
if args.outputinfo:
output_info(bins, args.type.split(","))
else:
error = make_organized_folder(
args.destination, bins, args.type.split(","))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment