Created
September 1, 2025 18:09
-
-
Save pagebase/c28a5d2ed9f49fa665d8cd26a2e2973d to your computer and use it in GitHub Desktop.
I have written this code to sort specified directory according their file extension such image into image directory, videos into videos directory so on. I have figured out while moving file which is already at destination, the file won't move there and give me error as `file exists`. To handle this error, I have added counter variable, but it gi…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import shutil | |
| try: | |
| # This function handle duplicate files | |
| def handle_duplicate_file(dst, directory_name, file_name, counter): | |
| name, ext=os.path.splitext(file_name) # Split file name extension in list as `["file_name", ".png"]` | |
| new_file_name=f"{file_name}_{counter}{ext}" | |
| return os.path.join(dst, directory_name, new_file_name) | |
| # Create directories function | |
| def create_directory(directory_names, dst): | |
| for i in directory_names: | |
| path = os.path.join(dst,i) # If source path is `C:\Sorted_Data` then `path` variable contains `C:\Sorted_Data\Photos`, `C:\Sorted_Data\Videos` etc | |
| os.makedirs(path, exist_ok=True) # 2nd parameter would not raise FileExistEror. If folder already exist then it will not create it and won't raise any error. | |
| def move_files(src, dst, file_ext_list, directory_name): | |
| for root, folders, files in os.walk(src): | |
| counter+=1 | |
| for file in files: | |
| if file.lower().endswith(tuple(file_ext_list)): # `endswith()` method takes `str` or `tuple of str` as argument. | |
| full_src_path=os.path.join(root,file) | |
| full_dst_path=os.path.join(dst,directory_name,file) | |
| if os.path.exists(full_dst_path): | |
| duplicate_full_path=handle_duplicate_file(dst, directory_name, file, counter) | |
| counter+=1 | |
| shutil.move(full_src_path, duplicate_full_path) | |
| shutil.move(full_src_path, full_dst_path) | |
| src=input("Enter source path: ").strip() # `strip()` method remove white spaces from left, right sides. | |
| dst=input("Enter destination path: ").strip() | |
| folder_names = ["Photos", "Videos", "Audio", "Documents", "Softwares", "Archives"] | |
| img_ext_list = [".png", ".jpeg", ".jpg", ".webp"] | |
| vid_ext_list = [".mp4", ".mkv", ".mov", ".wmv"] | |
| aud_ext_list = [".mp3", ".wav", ".aac"] | |
| doc_ext_list = [".pdf", ".csv", ".docx", "pptx", ".txt", ".md"] | |
| soft_ext_list=[".exe"] | |
| arc_ext_list=[".zip"] | |
| # Create directory function call | |
| create_directory(folder_names, dst) # Create dircetories at destination as "Photos", "Videos", "Audio", "Documents" | |
| # Move images function call | |
| move_files(src, dst, img_ext_list, "Photos") | |
| move_files(src, dst, vid_ext_list, "Videos") | |
| move_files(src, dst, aud_ext_list, "Audio") | |
| move_files(src, dst, doc_ext_list, "Documents") | |
| move_files(src, dst, soft_ext_list, "Softwares") | |
| move_files(src, dst, arc_ext_list, "Archives") | |
| except Exception as e: | |
| print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment