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