-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_organizer.py
More file actions
47 lines (33 loc) · 1.66 KB
/
Copy pathfile_organizer.py
File metadata and controls
47 lines (33 loc) · 1.66 KB
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
import os
import shutil # Shell utilities - for high level file and folder operation
def organize_files(source_folder):
#Define catagories
catagories ={
"Images": [".jpg",".jpeg", ".png",".gif"],
"Documents":[".pdf",".docx",".txt"],
"Videos":[".mp4",".mov",".avi"],
"Music":[".mp3",".wav"],
"Archives":[".zip",".rar",".tar"]
}
file_count = 0
#loop through files in source folder
for filename in os.listdir(source_folder):
file_path = os.path.join(source_folder,filename) #create path for file
if os.path.isfile(file_path): #check if file exist
file_ext = os.path.splitext(filename)[1].lower() # extract the extention
moved = False
for catagory, extension in catagories.items():
if file_ext in extension:
catagory_folder = os.path.join(source_folder,catagory) # create a new path of the catagory folder inside source folder
os.makedirs(catagory_folder, exist_ok=True) #Create the folder
shutil.move(file_path, os.path.join(catagory_folder, filename)) #move the file
file_count+=1
moved = True
break
if not moved:
# Put uncatagorized files in "others"
other_folder = os.path.join(source_folder, "Others") # Create path for "Others" folder
os.makedirs(other_folder, exist_ok = True) # create the folder from the path
shutil.move(file_path, os.path.join(other_folder, filename))
file_count+=1
return file_count