Skip to content

Commit 3632e88

Browse files
committed
Refactored code and logic
- Create only required directory - Added a few functions to make the code flow simple to follow
1 parent 46863f1 commit 3632e88

1 file changed

Lines changed: 52 additions & 19 deletions

File tree

filemover.py

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from os import path
33
import os
44

5-
65
print(r'''
76
______ __ ____ ____ __ ____ __
87
/ ____/____ ____/ /___ / __ \ ___ _____ / __/___ _____ / /_ / __ \ / /__ __ _____
@@ -25,7 +24,7 @@
2524
''')
2625

2726
folder_ex = {
28-
'Programming Files': set(['ipynb', 'py', 'java', 'cs', 'js', 'vsix', 'jar', 'cc', 'ccc', 'html', 'xml', 'kt']),
27+
'Programming Files': set(['ipynb', 'py', 'java', 'cs', 'js', 'vsix', 'jar', 'cc', 'ccc', 'html', 'xml', 'kt', 'c', 'css']),
2928
'Compressed': set(['zip', 'rar', 'arj', 'gz', 'sit', 'sitx', 'sea', 'ace', 'bz2', '7z']),
3029
'Applications': set(['exe', 'msi', 'deb', 'rpm']),
3130
'Pictures': set(['jpeg', 'jpg', 'png', 'gif', 'tiff', 'raw', 'webp', 'jfif', 'ico', 'psd', 'svg', 'ai']),
@@ -37,19 +36,35 @@
3736
}
3837

3938

40-
def create_folders():
41-
'''Creates the required folders to organize files ('Pictures', 'Videos'..).
39+
def create_folder(folder_name: str):
40+
'''
41+
Creates the folder
42+
43+
Args:
44+
folder_name (str): folder to be created
45+
'''
46+
try:
47+
os.mkdir(folder_name)
48+
print('{} Created ✔'.format(folder_name))
49+
except OSError:
50+
print('{} Already Exists'.format(folder_name))
51+
52+
53+
def move_files(file_folder_map: dict):
54+
'''
55+
Move files to respective folder
56+
57+
Args:
58+
ext_file_map (dict) : File to Folder map
4259
'''
43-
for root in folder_ex:
44-
try:
45-
os.mkdir(root)
46-
print(f'{root:20} Created ✔')
47-
except OSError:
48-
print(f'{root:20} Already Exists')
60+
for folder, files in file_folder_map.items():
61+
for file in files:
62+
move(file, folder)
4963

5064

5165
def get_folder(ext):
52-
'''Returns the Folder that corresponds to the given extension.
66+
'''
67+
Returns the Folder that corresponds to the given extension.
5368
5469
Args:
5570
ext (String): The extension of the file.
@@ -64,17 +79,35 @@ def get_folder(ext):
6479

6580

6681
def start():
67-
'''Organize files on the current directory, each to the corresponding folder.
6882
'''
83+
Organize files on the current directory, each to the corresponding folder.
84+
'''
85+
86+
file_folder_map = dict()
6987
for filename in os.listdir():
70-
# Check it's not filemover.py, a hidden file or a directory
71-
if filename != __file__ and filename[0] != '.' and '.' in filename:
72-
ext = os.path.basename(filename).split('.')[-1]
73-
folder = get_folder(ext)
74-
if not os.path.isfile(os.path.join(folder, filename)):
75-
move(filename, folder)
88+
# ignore filemover.py, hidden files or a directory
89+
if filename == os.path.basename(__file__) or filename[0] == '.' or '.' not in filename:
90+
continue
91+
92+
file_extension = os.path.basename(filename).split('.')[-1]
93+
folder = get_folder(file_extension)
94+
95+
# ignore files present in the folders
96+
if os.path.isfile(os.path.join(folder, filename)):
97+
continue
98+
99+
# insert file to file_folder_map
100+
if folder not in file_folder_map:
101+
file_folder_map[folder] = []
102+
file_folder_map[folder].append(filename)
103+
104+
# create required folders
105+
for folder in file_folder_map.keys():
106+
create_folder(folder)
107+
108+
# move files to folder
109+
move_files(file_folder_map)
76110

77111

78112
if __name__ == '__main__':
79-
create_folders()
80113
start()

0 commit comments

Comments
 (0)