Last active
June 12, 2021 11:58
-
-
Save Olgoetz/1a8c4eb185557bd750fbb1bcb2ff5e36 to your computer and use it in GitHub Desktop.
Automatically organise your 'downloads' folder with python3 (script may be invoked via a cronjob).
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
from pathlib import Path | |
# Set the path to 'downloads' directory | |
downloads = Path.home() / "downloads" | |
# File endings for different types | |
pictures = ['.jpg', '.png', '.gif', '.JPG', '.jpeg'] | |
media = ['.mp3', '.avi', '.mp4'] | |
documents = ['.pdf', '.doc', '.docx', '.txt', '.xls', '.xlsx'] | |
compressedFiles = ['.rar', '.zip'] | |
setupFiles = ['.exe', '.msi'] | |
# Locations for files | |
picturesLocation = downloads / "pictures" | |
mediaLocation = downloads / "media" | |
documentsLocation = downloads / "documents" | |
compressedFilesLocation = downloads / "compressedFiles" | |
setupFilesLocation = downloads / "setupFiles" | |
# Create folders | |
Path.mkdir(picturesLocation, parents=True, exist_ok=True) | |
Path.mkdir(mediaLocation, parents=True, exist_ok=True) | |
Path.mkdir(documentsLocation, parents=True, exist_ok=True) | |
Path.mkdir(compressedFilesLocation, parents=True, exist_ok=True) | |
Path.mkdir(setupFilesLocation, parents=True, exist_ok=True) | |
# Get all files from 'downloads' folder | |
files = Path.glob(downloads, "*") | |
# Sort the files based on their filetype (suffix) | |
for f in files: | |
if f.suffix in pictures: | |
Path(f).rename(picturesLocation / f.name) | |
if f.suffix in media: | |
Path(f).rename(mediaLocation / f.name) | |
if f.suffix in documents: | |
Path(f).rename(documentsLocation / f.name) | |
if f.suffix in compressedFiles: | |
Path(f).rename(compressedFilesLocation / f.name) | |
if f.suffix in setupFiles: | |
Path(f).rename(setupFilesLocation / f.name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment