Skip to content

Instantly share code, notes, and snippets.

@akarsh1995
Created July 20, 2020 15:26
Show Gist options
  • Select an option

  • Save akarsh1995/82ae4e31d9f62dbdd68caef44f10731b to your computer and use it in GitHub Desktop.

Select an option

Save akarsh1995/82ae4e31d9f62dbdd68caef44f10731b to your computer and use it in GitHub Desktop.
Group the directory's content using python
import shutil
from pathlib import Path
#General File Type Extentions
exts = {
"Applications": ['.exe'],
"Code": ['.py','.java','.c'],
"Music": ['.mp3','.ogg','.wav'],
}
# arranging in extension -> file types mapping
mapped_exts = {}
for ext_type, extenstions in exts.items():
for ext in extenstions:
mapped_exts[ext] = ext_type
#Check each file in the directory
def arrange_files(dir_path):
path = Path(dir_path)
for file in path.glob("*"):
extension = file.name.split('.')[-1]
# get the file type like for .exe extension filetype is Application
file_type = mapped_exts['.' + extension]
# create dir if not exists
file.parent.joinpath(file_type).mkdir(exist_ok=True)
# move the file into the desired dir
shutil.move(file, file.parent.joinpath(file_type, file.name))
if __name__ == "__main__":
directory = input("Enter the directory you want arranged.")
arrange_files(directory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment