Intro
Hello today we are going to code a files organizer using python script it will organize the files by their types for example it will put all files with extension (.png) in a folder called Pictures let's code ... (this post is published here Python files organizer, automate boring files organizing using python)
Coding
Importing needed modules
those are the modules we are going to use
if you have any problem you can post it on comments
import os
import pathlib
import shutil
import fnmatch
Preparing the default folder for every type of files
So now we are going to set a folder for every type for example we want to move the pictures to the folder in path '/home/rin/Downloads/'
in my case i am using linux on windows the paths will look like ' C:\rin\Brink\Pictures'
## define path for every category
file_folder={
'Music':'/home/rin/Downloads/',
'Pictures':'/home/rin/Pictures/',
'Documents':'/home/rin/Documents/',
'Archives':'/home/rin/Archives/',
'Videos':'/home/rin/Videos/',
'Codes':'/home/rin/Codes/'
}
Defining the type of file by the extension
What's extension ?
A file extension or file name extension is the ending of a file that helps identify the type of file in operating systems, such as Microsoft Windows. In Microsoft Windows, the file name extension is a period that is often followed by three characters but may also be one, two, or four characters long.
As an example, the file name "myfile.txt" has a file extension of ".txt," which is a file name extension associated with text files.
So code :
## define the type of every extension
file_type={'Pictures':['.jpg', '.jpeg', '.jpe','.png','.jp2','.ico', '.wbmp','.j2k', '.jpf', '.jpx', '.jpm', '.mj2','.svg', '.svgz','.webp','.gif']
,'Music':['mp3']
,'Videos':['.mp4','.srt','.mkv','.3gp','.m4a']
,'Documents':['.pdf','.docx','.doc','.csv','.txt','.xls','.xlsx','.log']
,'Archives':['.zip','.tar']
,'Codes':['.py']}
Main code
Now we take the path of the folder contains the files we want organize as example for me i want clean my download folder
path='/home/rin/Downloads/'
So now what we want to do is extracting all the files in the folder and check the type of every file then move it to the folder contains its type
files=os.listdir(path)# list of files in the folder
for file in files:#for for every file in the list
extension=pathlib.Path(file).suffix #extension of file ex:(.txt)
#Documents
if extension in file_type['Documents']:#check if the file is a document
move=pathlib.Path(path+file).rename(file_folder['Documents']+file)# move the file to the folder conatins documents
#move=shutil.move(path+file,file_folder['Documents'])
print('success #### {} to {}'.format(file,move))
#Music
if extension in file_type['Music']:
move=pathlib.Path(path+file).rename(file_folder['Music']+file)
#move=shutil.move(path+file,file_folder['Music'])
print('success #### {} to {}'.format(file,move))
#Pictures
if extension in file_type['Pictures']:
move=pathlib.Path(path+file).rename(file_folder['Pictures']+file)
#move=shutil.move(path+file,file_folder['Pictures'])
print('success #### {} to {}'.format(file,move))
#Videos
if extension in file_type['Videos']:
move=pathlib.Path(path+file).rename(file_folder['Videos']+file)
#move=shutil.move(path+file,file_folder['Videos'])
print('success #### {} to {}'.format(file,move))
#Archives
if extension in file_type['Archives']:
move=pathlib.Path(path+file).rename(file_folder['Archives']+file)
#move=shutil.move(path+file,file_folder['Archives'])
print('success #### {} to {}'.format(file,move))
#Codes
if extension in file_type['Codes']:
move=pathlib.Path(path+file).rename(file_folder['Codes']+file)
#move=shutil.move(path+file,file_folder['Codes'])
print('success #### {} to {}'.format(file,move))
else:
print('## '+file)
Note : You can add more types
Full source code
DON'T FORGET TO CHANGE THE PATHS ALSO YOU CAN ADD MORE CATEGORY BE SMART ..
import os
import pathlib
import shutil
import fnmatch
## define path for every category
file_folder={
'Music':'/home/rin/Downloads/',
'Pictures':'/home/rin/Pictures/',
'Documents':'/home/rin/Documents/',
'Archives':'/home/rin/Archives/',
'Videos':'/home/rin/Videos/',
'Codes':'/home/rin/Codes/'
}
## define the type of every extension
file_type={'Pictures':['.jpg', '.jpeg', '.jpe','.png','.jp2','.ico', '.wbmp','.j2k', '.jpf', '.jpx', '.jpm', '.mj2','.svg', '.svgz','.webp','.gif']
,'Music':['mp3']
,'Videos':['.mp4','.srt','.mkv','.3gp','.m4a']
,'Documents':['.pdf','.docx','.doc','.csv','.txt','.xls','.xlsx','.log']
,'Archives':['.zip','.tar']
,'Codes':['.py']}
path='/home/rin/Downloads/'
files=os.listdir(path)# list of files in the folder
for file in files:#for for every file in the list
extension=pathlib.Path(file).suffix #extension of file ex:(.txt)
#Documents
if extension in file_type['Documents']:#check if the file is a document
move=pathlib.Path(path+file).rename(file_folder['Documents']+file)# move the file to the folder conatins documents
#move=shutil.move(path+file,file_folder['Documents'])
print('success #### {} to {}'.format(file,move))
#Music
if extension in file_type['Music']:
move=pathlib.Path(path+file).rename(file_folder['Music']+file)
#move=shutil.move(path+file,file_folder['Music'])
print('success #### {} to {}'.format(file,move))
#Pictures
if extension in file_type['Pictures']:
move=pathlib.Path(path+file).rename(file_folder['Pictures']+file)
#move=shutil.move(path+file,file_folder['Pictures'])
print('success #### {} to {}'.format(file,move))
#Videos
if extension in file_type['Videos']:
move=pathlib.Path(path+file).rename(file_folder['Videos']+file)
#move=shutil.move(path+file,file_folder['Videos'])
print('success #### {} to {}'.format(file,move))
#Archives
if extension in file_type['Archives']:
move=pathlib.Path(path+file).rename(file_folder['Archives']+file)
#move=shutil.move(path+file,file_folder['Archives'])
print('success #### {} to {}'.format(file,move))
#Codes
if extension in file_type['Codes']:
move=pathlib.Path(path+file).rename(file_folder['Codes']+file)
#move=shutil.move(path+file,file_folder['Codes'])
print('success #### {} to {}'.format(file,move))
else:
print('## '+file)
Feel free to develop the code and post any problem or request in the comments
Top comments (3)
I would recommend to also loop over the keys and decide there instead of creating an if statement for every Directory.
that way it can be extended without touching more then neccessary :)
(and the code is shorter)
Yes good idea thanks
google python class
microsoft introduction to python course pythonandmltrainingcourses.com/cou...
Some comments have been hidden by the post's author - find out more