pythonbits

Automate File Organization using Python

Jan 9th, 2023
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | Source Code | 0 0
  1. # created by python bits. Check out my channel: https://www.youtube.com/@python-bits
  2.  
  3. import os
  4. from pathlib import Path
  5.  
  6. file_types = {
  7.  
  8.     'IMAGES':['.jpeg', 'jpg', '.tiff', '.gif', '.bmp', '.png', '.svg', '.heif', '.psd'],
  9.     'AUDIO':['.aac', '.aa', '.dvf', '.m4a', '.m4b', '.m4p', '.mp3', '.msv', '.raw', '.wav', '.wma'],
  10.     'VIDEOS':['.avi', 'flv', '.wmv', '.mov', '.mp4', '.webm', '.vob', '.mng', '.qt', '.mpg','.mpeg', '.3gp'],
  11.     'DOCOUMENTS':['.oxps','.epub', '.pages', '.docx', '.doc', '.fdf', '.ods', '.odt', '.pwi', '.xsn', '.xps', '.dotx', '.docm', '.dox', '.rvg', '.rtf', '.rtfd', '.wpd', '.xls', '.xlsx', '.ppt', '.pptx']
  12. }
  13.  
  14. dct = dict()
  15. for directory, file_formats in file_types.items():
  16.     for file_format in file_formats:
  17.         dct[file_format] = directory
  18.  
  19.  
  20. def file_organizer():
  21.     for entry in os.scandir():
  22.         if entry.is_dir():
  23.             continue
  24.        
  25.         file_path = Path(entry)
  26.         file_format = file_path.suffix.lower()
  27.  
  28.         if file_format in dct:
  29.             directory_path = Path(dct[file_format])
  30.             directory_path.mkdir(exist_ok=True)
  31.             file_path.rename(directory_path.joinpath(file_path))
  32.  
  33.         else:
  34.             others = Path('OTHERS')
  35.             others.mkdir(exist_ok=True)
  36.             file_path.rename(others.joinpath(file_path))
  37.  
  38.  
  39. if __name__ == '__main__':
  40.     file_organizer()
Advertisement
Add Comment
Please, Sign In to add comment