{"id":156,"date":"2025-10-27T16:19:37","date_gmt":"2025-10-27T16:19:37","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=156"},"modified":"2025-10-27T16:19:37","modified_gmt":"2025-10-27T16:19:37","slug":"21-real-world-python-projects-file-organizer-advanced","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=156","title":{"rendered":"21 &#8211; Real-World Python Projects &#8211; File Organizer (Advanced)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\ud83c\udfaf <strong>Project Objective<\/strong><\/h2>\n\n\n\n<p>To create an advanced <strong>File Organizer<\/strong> that automatically scans folders, classifies files by type, renames duplicates, logs actions, and can run on a schedule for continuous cleanup.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>Skills You\u2019ll Learn<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>File handling with <code>os<\/code> and <code>shutil<\/code><\/li>\n\n\n\n<li>Pattern matching using <code>fnmatch<\/code> and <code>re<\/code><\/li>\n\n\n\n<li>Logging and reporting<\/li>\n\n\n\n<li>Exception handling and automation<\/li>\n\n\n\n<li>Scheduling cleanups with <code>schedule<\/code><\/li>\n\n\n\n<li>Building modular, maintainable code<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f <strong>Technology Stack<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Library<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td><code>os<\/code>, <code>shutil<\/code><\/td><td>File and directory operations<\/td><\/tr><tr><td><code>re<\/code>, <code>fnmatch<\/code><\/td><td>Pattern matching for file extensions<\/td><\/tr><tr><td><code>time<\/code>, <code>datetime<\/code><\/td><td>Scheduling and timestamps<\/td><\/tr><tr><td><code>logging<\/code><\/td><td>Tracking all actions performed<\/td><\/tr><tr><td><code>pandas<\/code> <em>(optional)<\/em><\/td><td>Generating CSV reports<\/td><\/tr><tr><td><code>schedule<\/code> <em>(optional)<\/em><\/td><td>Automate periodic cleanups<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd27 <strong>Auto-Install Dependencies<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import subprocess, sys\n\ndef install(package):\n    try:\n        __import__(package)\n    except ImportError:\n        subprocess.check_call(&#91;sys.executable, \"-m\", \"pip\", \"install\", package])\n\nfor pkg in &#91;\"pandas\", \"schedule\"]:\n    install(pkg)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcc2 <strong>Step 1 \u2014 Directory Setup<\/strong><\/h2>\n\n\n\n<p>Create categorized folders automatically (if not present):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\n\nTARGET_DIR = \"C:\/Users\/YourName\/Downloads\"\nCATEGORIES = {\n    \"Images\": &#91;\".jpg\", \".jpeg\", \".png\", \".gif\", \".bmp\"],\n    \"Documents\": &#91;\".pdf\", \".docx\", \".txt\", \".xlsx\", \".pptx\"],\n    \"Videos\": &#91;\".mp4\", \".mov\", \".avi\", \".mkv\"],\n    \"Music\": &#91;\".mp3\", \".wav\", \".aac\"],\n    \"Archives\": &#91;\".zip\", \".rar\", \".tar\"],\n    \"Programs\": &#91;\".exe\", \".msi\"],\n    \"Scripts\": &#91;\".py\", \".js\", \".html\", \".css\"]\n}\n\nfor folder in CATEGORIES.keys():\n    path = os.path.join(TARGET_DIR, folder)\n    if not os.path.exists(path):\n        os.makedirs(path)\n<\/code><\/pre>\n\n\n\n<p>\u2705 Creates subfolders like \u201cImages\u201d, \u201cDocuments\u201d, etc.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f <strong>Step 2 \u2014 Smart File Sorting Function<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import shutil, re\nfrom datetime import datetime\n\ndef organize_files():\n    log_entries = &#91;]\n    for file_name in os.listdir(TARGET_DIR):\n        file_path = os.path.join(TARGET_DIR, file_name)\n        if os.path.isfile(file_path):\n            ext = os.path.splitext(file_name)&#91;1].lower()\n            moved = False\n\n            for category, extensions in CATEGORIES.items():\n                if ext in extensions:\n                    dest = os.path.join(TARGET_DIR, category)\n                    new_name = rename_if_duplicate(dest, file_name)\n                    shutil.move(file_path, os.path.join(dest, new_name))\n                    moved = True\n                    log_entries.append((file_name, category, datetime.now()))\n                    break\n\n            if not moved:\n                log_entries.append((file_name, \"Uncategorized\", datetime.now()))\n    \n    return log_entries\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>Step 3 \u2014 Handle Duplicate File Names<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>def rename_if_duplicate(dest, file_name):\n    base, ext = os.path.splitext(file_name)\n    counter = 1\n    new_name = file_name\n\n    while os.path.exists(os.path.join(dest, new_name)):\n        new_name = f\"{base}_{counter}{ext}\"\n        counter += 1\n    return new_name\n<\/code><\/pre>\n\n\n\n<p>\u2705 Automatically renames duplicates like <code>photo_1.jpg<\/code>, <code>photo_2.jpg<\/code>, etc.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddfe <strong>Step 4 \u2014 Logging Actions<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import logging\n\nlogging.basicConfig(\n    filename=\"file_organizer.log\",\n    level=logging.INFO,\n    format=\"%(asctime)s - %(levelname)s - %(message)s\"\n)\n\ndef log_activity(entries):\n    for file, category, timestamp in entries:\n        logging.info(f\"Moved: {file} \u2192 {category}\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcca <strong>Step 5 \u2014 Generate CSV Report<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\n\ndef export_report(entries):\n    df = pd.DataFrame(entries, columns=&#91;\"File\", \"Category\", \"Timestamp\"])\n    df.to_csv(\"organizer_report.csv\", index=False)\n    print(\"\u2705 Report saved as 'organizer_report.csv'\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd01 <strong>Step 6 \u2014 Automate Organization Periodically<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import schedule, time\n\ndef job():\n    print(\"Running file organizer...\")\n    entries = organize_files()\n    log_activity(entries)\n    export_report(entries)\n\n# Run every 10 minutes\nschedule.every(10).minutes.do(job)\n\nwhile True:\n    schedule.run_pending()\n    time.sleep(1)\n<\/code><\/pre>\n\n\n\n<p>\u2705 Your computer now auto-organizes files every 10 minutes.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf1 <strong>Full Combined Code (Advanced Organizer)<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import os, shutil, logging, pandas as pd, schedule, time\nfrom datetime import datetime\n\nTARGET_DIR = \"C:\/Users\/YourName\/Downloads\"\nCATEGORIES = {\n    \"Images\": &#91;\".jpg\", \".jpeg\", \".png\", \".gif\", \".bmp\"],\n    \"Documents\": &#91;\".pdf\", \".docx\", \".txt\", \".xlsx\", \".pptx\"],\n    \"Videos\": &#91;\".mp4\", \".mov\", \".avi\", \".mkv\"],\n    \"Music\": &#91;\".mp3\", \".wav\", \".aac\"],\n    \"Archives\": &#91;\".zip\", \".rar\", \".tar\"],\n    \"Programs\": &#91;\".exe\", \".msi\"],\n    \"Scripts\": &#91;\".py\", \".js\", \".html\", \".css\"]\n}\n\nfor folder in CATEGORIES.keys():\n    os.makedirs(os.path.join(TARGET_DIR, folder), exist_ok=True)\n\nlogging.basicConfig(filename=\"file_organizer.log\", level=logging.INFO, format=\"%(asctime)s - %(message)s\")\n\ndef rename_if_duplicate(dest, file_name):\n    base, ext = os.path.splitext(file_name)\n    counter = 1\n    new_name = file_name\n    while os.path.exists(os.path.join(dest, new_name)):\n        new_name = f\"{base}_{counter}{ext}\"\n        counter += 1\n    return new_name\n\ndef organize_files():\n    log_entries = &#91;]\n    for file_name in os.listdir(TARGET_DIR):\n        file_path = os.path.join(TARGET_DIR, file_name)\n        if os.path.isfile(file_path):\n            ext = os.path.splitext(file_name)&#91;1].lower()\n            moved = False\n            for category, extensions in CATEGORIES.items():\n                if ext in extensions:\n                    dest = os.path.join(TARGET_DIR, category)\n                    new_name = rename_if_duplicate(dest, file_name)\n                    shutil.move(file_path, os.path.join(dest, new_name))\n                    log_entries.append((file_name, category, datetime.now()))\n                    moved = True\n                    break\n            if not moved:\n                log_entries.append((file_name, \"Uncategorized\", datetime.now()))\n    return log_entries\n\ndef log_activity(entries):\n    for file, category, timestamp in entries:\n        logging.info(f\"Moved {file} \u2192 {category}\")\n\ndef export_report(entries):\n    df = pd.DataFrame(entries, columns=&#91;\"File\", \"Category\", \"Timestamp\"])\n    df.to_csv(\"organizer_report.csv\", index=False)\n\ndef job():\n    entries = organize_files()\n    log_activity(entries)\n    export_report(entries)\n    print(f\"\u2705 Organized at {datetime.now()}\")\n\nschedule.every(10).minutes.do(job)\n\nwhile True:\n    schedule.run_pending()\n    time.sleep(1)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udca1 <strong>Optional Enhancements<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>\ud83d\uddd1\ufe0f Trash Manager<\/td><td>Move deleted files to a \u201cRecycle Bin\u201d<\/td><\/tr><tr><td>\ud83d\udd0d Duplicate Detector<\/td><td>Use file hashes to detect duplicates<\/td><\/tr><tr><td>\ud83e\udde0 AI File Sorting<\/td><td>Use NLP to auto-categorize based on filenames<\/td><\/tr><tr><td>\ud83e\uddf0 GUI Interface<\/td><td>Add a Tkinter dashboard for manual control<\/td><\/tr><tr><td>\u2601\ufe0f Cloud Sync<\/td><td>Auto-sync organized files to Google Drive \/ Dropbox<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 <strong>Learning Outcomes<\/strong><\/h2>\n\n\n\n<p>\u2705 File handling and directory traversal<br>\u2705 Automated file management<br>\u2705 Logging and reporting<br>\u2705 Scheduling with Python<br>\u2705 Writing professional, modular code<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Project Objective To create an advanced File Organizer that automatically scans folders, classifies files by type, renames duplicates, logs actions, and can run on a schedule for continuous cleanup. \ud83e\udde9 Skills You\u2019ll Learn \u2699\ufe0f Technology Stack Library Purpose os, shutil File and directory operations re, fnmatch Pattern matching for file extensions time, datetime Scheduling [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-156","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=156"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/156\/revisions"}],"predecessor-version":[{"id":157,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/156\/revisions\/157"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}