Skip to content

Commit 0ac549b

Browse files
Merge pull request #6 from safeers/master
Optimised code in filemover.py
2 parents 02fd2a6 + 8f8c316 commit 0ac549b

2 files changed

Lines changed: 67 additions & 57 deletions

File tree

filemover.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
""")
2525

2626
folder_ex = {
27-
"Programming Files": [".ipynb", ".py", ".java", ".cs", ".js", ".vsix", ".jar"],
28-
"Compressed": [".zip", ".rar", ".arj", ".gz", ".sit", ".sitx", ".sea", ".ace", ".bz2", ".7z"],
29-
"Applications": [".exe", ".msi"],
30-
"Pictures": [".jpeg", ".jpg", ".png", ".gif", ".tiff", ".raw", ".webp", ".jfif", ".ico", ".psd", ".svg", ".ai"],
31-
"Videos": [".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", ],
32-
"Documents": [".txt", ".pdf", ".doc", ".xlsx", ".pdf", ".ppt", ".pps", ".docx", ".pptx"],
33-
"Music": [".mp3", ".wav", ".wma", ".mpa", ".ram", ".ra", ".aac", ".aif", ".m4a", ".tsa"],
34-
"Torrents": [".torrent"],
35-
"Other": [],
27+
"Programming Files": set([".ipynb", ".py", ".java", ".cs", ".js", ".vsix", ".jar"]),
28+
"Compressed": set([".zip", ".rar", ".arj", ".gz", ".sit", ".sitx", ".sea", ".ace", ".bz2", ".7z"]),
29+
"Applications": set([".exe", ".msi"]),
30+
"Pictures": set([".jpeg", ".jpg", ".png", ".gif", ".tiff", ".raw", ".webp", ".jfif", ".ico", ".psd", ".svg", ".ai"]),
31+
"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"]),
32+
"Documents": set([".txt", ".pdf", ".doc", ".xlsx", ".pdf", ".ppt", ".pps", ".docx", ".pptx"]),
33+
"Music": set([".mp3", ".wav", ".wma", ".mpa", ".ram", ".ra", ".aac", ".aif", ".m4a", ".tsa"]),
34+
"Torrents": set([".torrent"]),
35+
"Other": set([])
3636
}
3737

3838

filemovergui.py

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import os
2+
3+
import tkinter as tk
4+
from os import path
5+
from shutil import move
6+
17
print(r"""
28
______ __ ____ ____ __ ____ __
39
/ ____/____ ____/ /___ / __ \ ___ _____ / __/___ _____ / /_ / __ \ / /__ __ _____
@@ -17,59 +23,58 @@
1723
| // Website : http://codeperfectplus.herokuapp.com //|
1824
| --------------------------------------------------------------------------------------
1925
""")
20-
import os
21-
from os import path
22-
from shutil import move
23-
import tkinter as tk
2426

2527

26-
def create_folder():
27-
paths = [
28-
"Programming File",
29-
"Compressed",
30-
"Application",
31-
"Picture",
32-
"Video",
33-
"Documents",
34-
"Music",
35-
"Torrents"
36-
]
37-
for root in paths:
28+
folder_ex = {
29+
"Programming Files": set([".ipynb", ".py", ".java", ".cs", ".js", ".vsix", ".jar"]),
30+
"Compressed": set([".zip", ".rar", ".arj", ".gz", ".sit", ".sitx", ".sea", ".ace", ".bz2", ".7z"]),
31+
"Applications": set([".exe", ".msi"]),
32+
"Pictures": set([".jpeg", ".jpg", ".png", ".gif", ".tiff", ".raw", ".webp", ".jfif", ".ico", ".psd", ".svg", ".ai"]),
33+
"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"]),
34+
"Documents": set([".txt", ".pdf", ".doc", ".xlsx", ".pdf", ".ppt", ".pps", ".docx", ".pptx"]),
35+
"Music": set([".mp3", ".wav", ".wma", ".mpa", ".ram", ".ra", ".aac", ".aif", ".m4a", ".tsa"]),
36+
"Torrents": set([".torrent"]),
37+
"Other": set([])
38+
}
39+
40+
41+
def create_folders():
42+
"""Creates the required folders to organize files ('Pictures', 'Videos'...).
43+
"""
44+
for root in folder_ex:
3845
try:
3946
os.mkdir(root)
47+
print(f"'{root:20}' Created ✔")
4048
except OSError:
41-
print("Folder Already Exists")
42-
43-
pic = [".jpeg", ".jpg", ".png", ".gif", ".tiff", ".raw", ".webp", ".jfif", ".ico", ".psd", ".svg", ".ai"]
44-
pytho = [".ipynb", ".java", ".cs", ".js", ".vsix",".jar"]
45-
txt = [".txt", ".pdf", ".doc", ".pdf", ".ppt", ".pps", ".docx", ".pptx"]
46-
music = [".mp3", ".wav", ".wma", ".mpa", ".ram", ".ra", ".aac", ".aif", ".m4a", ".tsa"]
47-
zip = [".zip", ".rar", ".arj", ".gz", ".sit", ".sitx", ".sea", ".ace", ".bz2", ".7z"]
48-
app = [".exe", ".msi"]
49-
vid = [".mp4",".webm",".mkv",".MPG",".MP2",".MPEG",".MPE",".MPV",".OGG",".M4P",".M4V",
50-
".WMV",".MOV",".QT",".FLV",".SWF",".AVCHD",".avi",".mpg",".mpe",".mpeg",".asf",
51-
".wmv",".mov",".qt",".rm",]
52-
torrents = [".torrent"]
53-
54-
def transfer(arr,name,ex,file):
55-
for i in range(len(arr)):
56-
if ex == arr[i]:
57-
move(file,name)
49+
print(f"'{root:20}' Already Exists")
50+
51+
52+
def get_folder(ext):
53+
"""Returns the Folder that corresponds to the given extension.
54+
55+
Args:
56+
ext (String): The extension of the file.
57+
58+
Returns:
59+
String: The name of the Folder that holds the ext.
60+
"""
61+
for f, ex in folder_ex.items():
62+
if ext in ex:
63+
return f
64+
return "Other"
65+
66+
5867

5968
def start():
60-
for files in os.listdir():
61-
try:
62-
_, ex = path.splitext(files)
63-
transfer(pic,"Picture",ex,files)
64-
transfer(vid,"Video",ex,files)
65-
transfer(pytho,"Programming File",ex,files)
66-
transfer(txt,"Documents",ex,files)
67-
transfer(music,"Music",ex,files)
68-
transfer(torrents,"Torrents",ex,files)
69-
transfer(txt,"Application",ex,files)
70-
transfer(zip,"Compressed",ex,files)
71-
except:
72-
print("Couldn't move file ", files)
69+
for file in os.listdir():
70+
# Check it's not filemover.py, a hidden file or a directory
71+
if file != __file__ and file[0] != '.' and path.isfile(file):
72+
try:
73+
_, ex = path.splitext(file)
74+
folder = get_folder(ex)
75+
move(file, folder)
76+
except:
77+
print("Couldn't move file ", file)
7378

7479

7580
if __name__ == "__main__":
@@ -91,13 +96,18 @@ def start():
9196
btn = tk.Button(
9297
master=frame_a,
9398
text="Create Folder",
94-
command=create_folder,
99+
command=create_folders,
95100
bg="green",
96101
width=20,
97102
height=5,
98103
)
99104
btn2 = tk.Button(
100-
master=frame_b, text="Move Now", command=start, bg="red", width=20, height=5
105+
master=frame_b,
106+
text="Move Now",
107+
command=start,
108+
bg="red",
109+
width=20,
110+
height=5
101111
)
102112
lbl = tk.Label(
103113
master=window,

0 commit comments

Comments
 (0)