🤖 Automation to cleanup your desktop


Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python.

Hope your weekend was great! I am sure we didn't waste it organizing the abundant files on our computers. There is a simple automation in Python that does this in few lines of code.

🌱 Import/Load needed modules

We covered shutil on Friday, and os earlier.

In: import os, shutil
Out:

🎯 Select folder that needs cleanup

In:
desktop = os.path.expanduser("~/Desktop")
print(desktop)
Out: /home/user/Desktop

✏️ Classify file types into categories

can add more categories as needed (music, excel/csv)

In:
file_types = {
  "Images": [".png", ".jpg", ".jpeg"],
  "Docs": [".pdf", ".docx", ".txt"],
  "Others": []
}
Out:

🛠️ Create sub folders for each category

In:
# makedirs creates folders
# exist_ok avoids errors if folder is already present

for folder in list(file_types.keys()):
  os.makedirs(os.path.join(desktop, folder), exist_ok=True)
Out:

🪄 ✨ Below is where the magic happens

In:
# 1.Loop through all items in Desktop
for file in os.listdir(desktop):
    path = os.path.join(desktop, file)

# 2.Only move files, not sub folders
    if os.path.isfile(path):


# 3.Move when extension of file matches to extensions in dictionary
        for folder, exts in file_types.items():
            if file.endswith(tuple(exts)):
                shutil.move(path, os.path.join(desktop, folder, file))
                break

# 4.Move to "Others" if no match
        else:
            shutil.move(path, os.path.join(desktop, "Others", file))
Out:

⭐📣 That's it for today! If you liked it, please share it with anyone who will find it useful and share your feedback below 🐼

Pandas Daily

Beginner to Expert in Python in just 5 minutes

Read more from Pandas Daily

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. I am sure when you print C:\new_folder your code breaks due to \. Or you wonder how to print single (It's) or double ("Hello") quotes. Enter Escape Sequences, tiny backslash commands in Python that fix all of this. 👉 Most common uses: Format using new lines Print tabs Single or double quotes in text Write file paths safely Print symbols like π or❤️ Let's Begin... 👇 \n Print in next line - the one we all...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Think about the time you waste right-clicking and saving course lectures on YouTube one-by-one. Inefficient process, can cause errors and you lose focus. pytubefix eliminates that bottleneck. Today is day 3 of mastering YouTube with Python. You already know how to get stats of a single video; or download them in various formats. Now time to level up with playlists. Pytubefix has a Playlist object that...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Yesterday we introduced pytubefix - a powerful module to access YouTube. We downloaded a video as well. Cool, but basic. What if you want 1080p instead of 480p. Or prefer WebM over MP4. To answer all this - let's cover downloads in depth today. 🤫 I am 100% sure you can charm recruiters or your colleagues as they won't know this! 📥 Code Recap: How to download YouTube video In: # Import library from...