|
| 1 | + |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Created on Thu Feb 14 18:23:10 2019 |
| 5 | +
|
| 6 | +@author: arghac14 |
| 7 | +""" |
| 8 | + |
| 9 | +# cook your dish here |
| 10 | +import os |
| 11 | +import time |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +def createDir(BASE_DIR,dn): |
| 15 | + for i in range(dn): |
| 16 | + os.mkdir(BASE_DIR + str(i) + 'Folder') |
| 17 | + |
| 18 | +def createFile(BASE_DIR,fn): |
| 19 | + for i in range(fn): |
| 20 | + f=open(BASE_DIR + str(i) + 'File.txt','w') |
| 21 | + f.close() |
| 22 | + |
| 23 | +def fileCounter(BASE_DIR): |
| 24 | + fileCount = 0 |
| 25 | + dirCount = 0 |
| 26 | + |
| 27 | + for root, dirs, files in os.walk(BASE_DIR): |
| 28 | + print('Looking in:',root) |
| 29 | + for directories in dirs: |
| 30 | + dirCount += 1 |
| 31 | + for Files in files: |
| 32 | + fileCount += 1 |
| 33 | + |
| 34 | + print('Number of files: ',fileCount) |
| 35 | + print('Number of Directories: ',dirCount) |
| 36 | + print('Total: ',(dirCount + fileCount)) |
| 37 | + |
| 38 | +def searchFile(BASE_DIR,ss): |
| 39 | + |
| 40 | + for root, dirs, files in os.walk(BASE_DIR): |
| 41 | + print('Looking in:',root) |
| 42 | + for Files in files: |
| 43 | + |
| 44 | + try: |
| 45 | + found = Files.find(ss) |
| 46 | + if found != -1: |
| 47 | + print(ss, 'Found!') |
| 48 | + break |
| 49 | + elif found == -1: |
| 50 | + print("File not fount!") |
| 51 | + else: |
| 52 | + print("Something went wrong!") |
| 53 | + except: |
| 54 | + print('Something went wrong! Try again..') |
| 55 | + exit() |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +DIRECTORIES = { |
| 61 | + "HTML": [".html5", ".html", ".htm", ".xhtml"], |
| 62 | + "IMAGES": [".jpeg", ".jpg", ".tiff", ".gif", ".bmp", ".png", ".bpg", "svg", |
| 63 | + ".heif", ".psd"], |
| 64 | + "VIDEOS": [".avi", ".flv", ".wmv", ".mov", ".mp4", ".webm", ".vob", ".mng", |
| 65 | + ".qt", ".mpg", ".mpeg", ".3gp", ".mkv"], |
| 66 | + "DOCUMENTS": [".oxps", ".epub", ".pages", ".docx", ".doc", ".fdf", ".ods", |
| 67 | + ".odt", ".pwi", ".xsn", ".xps", ".dotx", ".docm", ".dox", |
| 68 | + ".rvg", ".rtf", ".rtfd", ".wpd", ".xls", ".xlsx", ".ppt", |
| 69 | + "pptx"], |
| 70 | + "ARCHIVES": [".a", ".ar", ".cpio", ".iso", ".tar", ".gz", ".rz", ".7z", |
| 71 | + ".dmg", ".rar", ".xar", ".zip"], |
| 72 | + "AUDIO": [".aac", ".aa", ".aac", ".dvf", ".m4a", ".m4b", ".m4p", ".mp3", |
| 73 | + ".msv", "ogg", "oga", ".raw", ".vox", ".wav", ".wma"], |
| 74 | + "PLAINTEXT": [".txt", ".in", ".out"], |
| 75 | + "PDF": [".pdf"], |
| 76 | + "PYTHON": [".py"], |
| 77 | + "XML": [".xml"], |
| 78 | + "EXE": [".exe"], |
| 79 | + "SHELL": [".sh"] |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +FILE_FORMATS = {file_format: directory |
| 85 | + for directory, file_formats in DIRECTORIES.items() |
| 86 | + for file_format in file_formats} |
| 87 | + |
| 88 | + |
| 89 | +def organize(): |
| 90 | + DIRECTORIES = { |
| 91 | + "HTML": [".html5", ".html", ".htm", ".xhtml"], |
| 92 | + "IMAGES": [".jpeg", ".jpg", ".tiff", ".gif", ".bmp", ".png", ".bpg", "svg", |
| 93 | + ".heif", ".psd"], |
| 94 | + "VIDEOS": [".avi", ".flv", ".wmv", ".mov", ".mp4", ".webm", ".vob", ".mng", |
| 95 | + ".qt", ".mpg", ".mpeg", ".3gp", ".mkv"], |
| 96 | + "DOCUMENTS": [".oxps", ".epub", ".pages", ".docx", ".doc", ".fdf", ".ods", |
| 97 | + ".odt", ".pwi", ".xsn", ".xps", ".dotx", ".docm", ".dox", |
| 98 | + ".rvg", ".rtf", ".rtfd", ".wpd", ".xls", ".xlsx", ".ppt", |
| 99 | + "pptx"], |
| 100 | + "ARCHIVES": [".a", ".ar", ".cpio", ".iso", ".tar", ".gz", ".rz", ".7z", |
| 101 | + ".dmg", ".rar", ".xar", ".zip"], |
| 102 | + "AUDIO": [".aac", ".aa", ".aac", ".dvf", ".m4a", ".m4b", ".m4p", ".mp3", |
| 103 | + ".msv", "ogg", "oga", ".raw", ".vox", ".wav", ".wma"], |
| 104 | + "PLAINTEXT": [".txt", ".in", ".out"], |
| 105 | + "PDF": [".pdf"], |
| 106 | + "PYTHON": [".py"], |
| 107 | + "XML": [".xml"], |
| 108 | + "EXE": [".exe"], |
| 109 | + "SHELL": [".sh"] |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + FILE_FORMATS = {file_format: directory |
| 115 | + for directory, file_formats in DIRECTORIES.items() |
| 116 | + for file_format in file_formats} |
| 117 | + for entry in os.scandir(): |
| 118 | + if entry.is_dir(): |
| 119 | + continue |
| 120 | + file_path = Path(entry.name) |
| 121 | + file_format = file_path.suffix.lower() |
| 122 | + if file_format in FILE_FORMATS: |
| 123 | + directory_path = Path(FILE_FORMATS[file_format]) |
| 124 | + directory_path.mkdir(exist_ok=True) |
| 125 | + file_path.rename(directory_path.joinpath(file_path)) |
| 126 | + |
| 127 | + try: |
| 128 | + os.mkdir("OTHER-FILES") |
| 129 | + except: |
| 130 | + pass |
| 131 | + |
| 132 | + for dir in os.scandir(): |
| 133 | + try: |
| 134 | + if dir.is_dir(): |
| 135 | + os.rmdir(dir) |
| 136 | + else: |
| 137 | + os.rename(os.getcwd() + '/' + str(Path(dir)), os.getcwd() + '/OTHER-FILES/' + str(Path(dir))) |
| 138 | + except: |
| 139 | + pass |
| 140 | + |
| 141 | + |
| 142 | +if __name__ =='__main__': |
| 143 | + #BASE_DIR ='C:/Users/User/Desktop/fileman/' |
| 144 | + BASE_DIR=input("Enter the absolute path of the location where the operations are going to take place [eg: C:/Users/User/Desktop/fileman/ ] : ") |
| 145 | + print("|| 1.Create Directories | 2.Create Files | 3.Organize Files | 4.Count Files & Directories | 5. Search files ||") |
| 146 | + op=int(input("Choose an option: ")) |
| 147 | + if(op==1): |
| 148 | + dn=int(input("How many directories you want to create? : ")) |
| 149 | + createDir(BASE_DIR,dn) |
| 150 | + time.sleep(2) |
| 151 | + print("Process completed!") |
| 152 | + elif(op==2): |
| 153 | + fn=int(input("How many files you want to create? : ")) |
| 154 | + createFile(BASE_DIR,fn) |
| 155 | + time.sleep(2) |
| 156 | + print("Process completed!") |
| 157 | + elif(op==3): |
| 158 | + organize() |
| 159 | + time.sleep(2) |
| 160 | + print("Done") |
| 161 | + elif(op==4): |
| 162 | + fileCounter(BASE_DIR) |
| 163 | + elif(op==5): |
| 164 | + ss=input("Enter the exact file name with extenstion: ") |
| 165 | + searchFile(BASE_DIR,ss) |
| 166 | + else: |
| 167 | + print("Invalid Option!") |
0 commit comments