|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +import sys |
| 4 | + |
| 5 | +# Taking input |
| 6 | +print_string = """ |
| 7 | +Type Path of the directory |
| 8 | +OR |
| 9 | +Press enter for running the script on current directory: |
| 10 | +OR |
| 11 | +Type quit |
| 12 | +""" |
| 13 | +print(print_string+"\n\n") |
| 14 | +input_path = input("Input:") |
| 15 | +print("\n\n") |
| 16 | + |
| 17 | +# Script will terminate if input is 'quit' |
| 18 | +if input_path == "quit": |
| 19 | + sys.exit(1) |
| 20 | + |
| 21 | +# If nothing is entered then current working directory will be taken as the input path |
| 22 | +if input_path == "": |
| 23 | + input_path = os.getcwd() |
| 24 | + |
| 25 | +input_path = Path(input_path) |
| 26 | + |
| 27 | +# Changing the working directory to input path |
| 28 | +os.chdir(input_path) |
| 29 | + |
| 30 | +# Creates a dictionary "dic" with key,value pairs where key is extension and value is no. of files with that extension |
| 31 | +dic = {} |
| 32 | +for file in os.listdir(os.getcwd()): |
| 33 | + if os.path.isfile(file): |
| 34 | + extension = file.split(".")[-1] |
| 35 | + dic[extension] = dic.get(extension, 0)+1 |
| 36 | + |
| 37 | +for key in dic: |
| 38 | + print(f"There are {dic[key]} files file with extension {key}") |
| 39 | +print("\n\n") |
| 40 | + |
| 41 | +# assigning a variable named current Path of current working directory just for simplicity. |
| 42 | +# could have used input_path too |
| 43 | +current = Path(os.getcwd()) |
| 44 | + |
| 45 | +''' |
| 46 | +When this script would run the structure of the current directory would change.Hence, |
| 47 | +we are assigning list_dir variable the files and dirs in current working directory which the script would modify |
| 48 | +''' |
| 49 | +list_dir = os.listdir(current) |
| 50 | + |
| 51 | + |
| 52 | +# keys of dic are extensions of the file |
| 53 | +for key in dic: |
| 54 | + # try except block for making directory if it doesn't exists already |
| 55 | + try: |
| 56 | + os.mkdir(key) |
| 57 | + except: |
| 58 | + print(f"directory named {key} already exists so it won't be overwrited \n") |
| 59 | + |
| 60 | + # goes through the files in list_dir |
| 61 | + # we are not using os.listdir() as the directory structure will change during the execution |
| 62 | + for file in list_dir: |
| 63 | + if file.split(".")[-1] == key and os.path.isfile(file): |
| 64 | + # prints absolute path of the file |
| 65 | + print(os.path.abspath(file)) |
| 66 | + # Renames the path of the file or moves the file in to the newly created directory |
| 67 | + Path.rename(Path(os.path.abspath(file)), current / |
| 68 | + Path("./{}/".format(key)+file)) |
| 69 | + |
| 70 | +# This block just prints a note and the current structure of the directory |
| 71 | + |
| 72 | +print("\n Script has organised files as per their extensions into different directories! \n") |
| 73 | +for file in os.listdir(os.getcwd()): |
| 74 | + if not(os.path.isfile(file)): |
| 75 | + print(file) |
0 commit comments