Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File organizer #77

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions File Organizer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# File Oragnizer
The python script organizes files for a given directory which organizes according to the file type.<br>
You can update the different file types by editing the file_organizer.py under the variable name `file_types`
49 changes: 49 additions & 0 deletions File Organizer/file_organizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os
import shutil

# Define the directory to organize
root_dir = input("Enter Path Directory: ")

# Define the file types and their corresponding subdirectories
file_types = {
'images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp'],
'documents': ['.pdf', '.doc', '.docx', '.txt', '.xlsx', 'csv'],
'videos': ['.mp4', '.mkv', '.avi', '.mov'],
'audio': ['.mp3', '.wav', '.ogg'],
'archives': ['.zip', '.rar', '.7z', '.tar'],
'others': []
}

def organize_files(root_dir):
# Create subdirectories if they don't exist
for subdirectory in file_types.keys():
subdirectory_path = os.path.join(root_dir, subdirectory)
if not os.path.exists(subdirectory_path):
os.makedirs(subdirectory_path)

# Iterate through all files in the root directory
for filename in os.listdir(root_dir):
file_path = os.path.join(root_dir, filename)

# Skip if it's a directory
if os.path.isdir(file_path):
continue

# Get the file extension
file_extension = os.path.splitext(file_path)[1].lower()

# Determine the subdirectory based on the file extension
for subdirectory, extensions in file_types.items():
if file_extension in extensions:
subdirectory_path = os.path.join(root_dir, subdirectory)
shutil.move(file_path, subdirectory_path)
print(f"Moved '{filename}' to '{subdirectory}'")
break
else:
# If the file type is not recognized, move it to 'others'
subdirectory_path = os.path.join(root_dir, 'others')
shutil.move(file_path, subdirectory_path)
print(f"Moved '{filename}' to 'others'")

if __name__ == "__main__":
organize_files(root_dir)
Loading