Skip to content

Commit de9f826

Browse files
authored
Merge pull request hastagAB#264 from codePerfectPlus/directory_organizer
updating functionlity for directory organizer
2 parents c4bef27 + 6aa0a7b commit de9f826

File tree

1 file changed

+56
-56
lines changed

1 file changed

+56
-56
lines changed
Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
11
#!/usr/bin/python3
22

3-
import argparse
3+
from shutil import move
4+
from os import path
45
import os
56

67

7-
def path():
8-
parse = argparse.ArgumentParser(
9-
add_help=True, description="Organize your files to different directories according to their type")
10-
parse.add_argument('directory_path', type=str, default='./',
11-
help="The absolute path to the directory")
12-
return parse.parse_args().directory_path
13-
14-
15-
documents = ['.log', '.txt', '.doc', '.docx', '.md', '.pdf', '.wps']
16-
picture = ['.png', '.jpg', 'jpeg', '.bmp']
17-
music = ['.mp3', '.wav']
18-
compressed = ['.zip', '.rar', '.tar', '.gz', '.bz2', '.xz']
19-
video = ['.3gp', '.mov', '.mp4', '.mkv', '.srt', '.avi']
20-
web = ['.html', .'.css', '.js']
21-
source = ['.py', '.c', '.cpp', '.java',]
22-
23-
24-
directories = [path() + '/Compressed', path() + '/Documents',
25-
path() + '/Pictures', path() + '/Music', path() + '/Video', path() + '/Web', path() + '/Source-codes',]
26-
27-
print("This will organize your files to different directories according to their type!!")
28-
print("Are you sure you want to continue? (y/n)")
29-
flag = input('>>>')
30-
if flag.lower() == 'y':
31-
try:
32-
for d in directories:
33-
os.mkdir(d)
34-
except FileExistsError:
35-
pass
36-
37-
for files in os.listdir(path()):
38-
dot = (files.rfind('.'))
39-
if dot is not 0 and dot is not -1:
40-
if files[dot:].lower() in music:
41-
os.rename(path() + '/' + files, path() + '/Music/' + files)
42-
if files[dot:].lower() in picture:
43-
os.rename(path() + '/' + files, path() + '/Pictures/' + files)
44-
if files[dot:].lower() in documents:
45-
os.rename(path() + '/' + files, path() + '/Documents/' + files)
46-
if files[dot:].lower() in compressed:
47-
os.rename(path() + '/' + files, path() +
48-
'/Compressed/' + files)
49-
if files[dot:].lower() in video:
50-
os.rename(path() + '/' + files, path() + '/Video/' + files)
51-
if files[dot:].lower() in web:
52-
os.rename(path() + '/' + files, path() + '/Web/' + files)
53-
if files[dot:].lower() in source:
54-
os.rename(path() + '/' + files, path() + '/Source-codes/' + files)
55-
56-
for d in directories:
57-
if os.listdir(d) is None:
58-
os.removedirs(d)
59-
else:
60-
print("Exiting")
61-
os.sys.exit(0)
8+
folder_ex = {
9+
'Programming Files': set(['ipynb', 'py', 'java', 'cs', 'js', 'vsix', 'jar', 'cc', 'ccc', 'html', 'xml', 'kt']),
10+
'Compressed': set(['zip', 'rar', 'arj', 'gz', 'sit', 'sitx', 'sea', 'ace', 'bz2', '7z']),
11+
'Applications': set(['exe', 'msi', 'deb', 'rpm']),
12+
'Pictures': set(['jpeg', 'jpg', 'png', 'gif', 'tiff', 'raw', 'webp', 'jfif', 'ico', 'psd', 'svg', 'ai']),
13+
'Videos': set(['mp4', 'webm', 'mkv', 'MPG', 'MP2', 'MPEG', 'MPE', 'MPV', 'OGG', 'M4P', 'M4V', 'WMV', 'MOV', 'QT', 'FLV', 'SWF', 'AVCHD', 'avi', 'mpg', 'mpe', 'mpeg', 'asf', 'wmv', 'mov', 'qt', 'rm']),
14+
'Documents': set(['txt', 'pdf', 'doc', 'xlsx', 'pdf', 'ppt', 'pps', 'docx', 'pptx']),
15+
'Music': set(['mp3', 'wav', 'wma', 'mpa', 'ram', 'ra', 'aac', 'aif', 'm4a', 'tsa']),
16+
'Torrents': set(['torrent']),
17+
'Other': set([])
18+
}
19+
20+
21+
def create_folders():
22+
'''Creates the required folders to organize files ('Pictures', 'Videos'..).
23+
'''
24+
for root in folder_ex:
25+
try:
26+
os.mkdir(root)
27+
print(f'{root:20} Created ✔')
28+
except OSError:
29+
print(f'{root:20} Already Exists')
30+
31+
32+
def get_folder(ext):
33+
'''Returns the Folder that corresponds to the given extension.
34+
35+
Args:
36+
ext (String): The extension of the file.
37+
38+
Returns:
39+
String: The name of the Folder that holds the ext.
40+
'''
41+
for f, ex in folder_ex.items():
42+
if ext in ex:
43+
return f
44+
return 'Other'
45+
46+
47+
def start():
48+
'''Organize files on the current directory, each to the corresponding folder.
49+
'''
50+
for filename in os.listdir():
51+
# Check it's not filemover.py, a hidden file or a directory
52+
if filename != __file__ and filename[0] != '.' and '.' in filename:
53+
ext = os.path.basename(filename).split('.')[-1]
54+
folder = get_folder(ext)
55+
if not os.path.isfile(os.path.join(folder, filename)):
56+
move(filename, folder)
57+
58+
59+
if __name__ == '__main__':
60+
create_folders()
61+
start()

0 commit comments

Comments
 (0)