Created
June 18, 2023 13:19
-
-
Save ClimenteA/3326b427a2a79265d44c35d54850c345 to your computer and use it in GitHub Desktop.
Useful python script which can be used to organize your archive of photos, videos, documents etc
This file contains 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 | |
fileFormat = { | |
"Picture": [ | |
".jpeg", | |
".jpg", | |
".gif", | |
".bmp", | |
".png", | |
], | |
"Video": [ | |
".avi", | |
".mkv", | |
".flv", | |
".wmv", | |
".mov", | |
".mp4", | |
".webm", | |
".vob", | |
".mng", | |
".qt", | |
".mpg", | |
".mpeg", | |
".3gp", | |
], | |
"Document": [ | |
".oxps", | |
".epub", | |
".pages", | |
".docx", | |
".txt", | |
".pdf", | |
".doc", | |
".fdf", | |
".ods", | |
".odt", | |
".pwi", | |
".xsn", | |
".xps", | |
".dotx", | |
".docm", | |
".dox", | |
".rvg", | |
".rtf", | |
".rtfd", | |
".wpd", | |
".xls", | |
".xlsx", | |
".ppt", | |
"pptx", | |
], | |
"Compressed": [ | |
".a", | |
".ar", | |
".cpio", | |
".iso", | |
".tar", | |
".gz", | |
".rz", | |
".7z", | |
".dmg", | |
".rar", | |
".xar", | |
".zip", | |
], | |
"Audio": [ | |
".aac", | |
".aa", | |
".aac", | |
".dvf", | |
".m4a", | |
".m4b", | |
".m4p", | |
".mp3", | |
".msv", | |
"ogg", | |
"oga", | |
".raw", | |
".vox", | |
".wav", | |
".wma", | |
], | |
} | |
def get_all_files(directory): | |
file_list = [] | |
for root, dirs, files in os.walk(directory): | |
for file in files: | |
file_path = os.path.join(root, file) | |
file_list.append(file_path) | |
return file_list | |
def group_files_by_extension(file_list): | |
file_groups = {"Other": []} | |
for file_path in file_list: | |
_, file_extension = os.path.splitext(file_path) | |
file_extension = file_extension.lower() | |
group_found = False | |
for group, extensions in fileFormat.items(): | |
if file_extension in extensions: | |
if group not in file_groups: | |
file_groups[group] = [] | |
file_groups[group].append(file_path) | |
group_found = True | |
break | |
if not group_found: | |
file_groups["Other"].append(file_path) | |
return file_groups | |
def move_files_to_archive(file_groups, archive_dir): | |
for group, files in file_groups.items(): | |
group_dir = os.path.join(archive_dir, group) | |
os.makedirs(group_dir, exist_ok=True) | |
for file_path in files: | |
file_name = os.path.basename(file_path) | |
new_file_path = os.path.join(group_dir, file_name) | |
# Move the file to the group's folder | |
shutil.move(file_path, new_file_path) | |
print(f"Moved {file_path} to {new_file_path}") | |
if __name__ == "__main__": | |
# Usage example: | |
directory_path = "/home/alin/Downloads/drive/unordered" | |
archive_dir = "/home/alin/Downloads/drive/arhiva" | |
files = get_all_files(directory_path) | |
file_groups = group_files_by_extension(files) | |
move_files_to_archive(file_groups, archive_dir) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment