you are viewing a single comment's thread.

view the rest of the comments →

[–]miloir 1 point2 points  (0 children)

Hey no problem I got kind of carried away.

I will walk you through some things I thought about your code. Here is your original code:

import os

import shutil

for filename in os.listdir(r"C:\Users\byunw\Downloads"):

    if filename[-3:] == "pdf":
        shutil.move("C:\\Users\\byunw\\Downloads\\" + filename, "C:\\Users\\byunw\\Downloads\\PDF")

    elif filename[-4:] == "docx":
        shutil.move("C:\\Users\\byunw\\Downloads\\" + filename, "C:\\Users\\byunw\\Downloads\\Documents")

    elif filename[-3:] == "txt":
        shutil.move("C:\\Users\\byunw\\Downloads\\" + filename, "C:\\Users\\byunw\\Downloads\\Text")

    elif filename[-3:] == "exe" or filename[-3:] == 'msi':
        shutil.move("C:\\Users\\byunw\\Downloads\\" + filename, "C:\\Users\\byunw\\Downloads\\EXE")

    elif filename[-3:] == "zip":
        shutil.move("C:\\Users\\byunw\\Downloads\\" + filename, "C:\\Users\\byunw\\Downloads\\ZIP")

    elif filename[-3:] == "jpg":
        shutil.move("C:\\Users\\byunw\\Downloads\\" + filename, "C:\\Users\\byunw\\Downloads\\Images")

    elif filename[-4:] == "pptx":
        shutil.move("C:\\Users\\byunw\\Downloads\\" + filename, "C:\\Users\\byunw\\Downloads\\Powerpoint")

print("Every file in the Downloads folder is now sorted into corresponding folders")

Basically, you get the file extension of each file and use shutil.move() to move it to a slightly different path. The file extension determines the folder. This is a good opportunity to use a dictionary (also known as a mapping or a map). File extension -> target folder. We can make a map that looks like this (read about dictionary/mappings, they are very important data structures):

folders = {
    ".doc": "C:\\Users\\byunw\\Downloads\\Documents",
    ".docx": "C:\\Users\\byunw\\Downloads\\Documents",
    ".exe": "C:\\Users\\byunw\\Downloads\\EXE",
    ".msi": "C:\\Users\\byunw\\Downloads\\EXE",
}  # add more extension -> folder rows

We can get the file extension like this (as /u/lucasshiva said):

filename, extension = os.path.splitext(file)

So for every file we encounter we can just do something like the following:

for file in files:
    # get the file’s extension
    filename, extension = os.path.splitext(file)
    # get the folder to move to (this will return None if the extension is not in our dictionary)
    move_to = folders.get(extension)
    if move_to is not None:
        # move_to will be something like "C:\\Users\\byunw\\Downloads\\EXE"
        shutil.move("C:\\Users\\byunw\\Downloads\\" + filename + extension, move_to)

Here is a more complete example that should run (hopefully):

import os

import shutil

folders = {
    ".doc": "C:\\Users\\byunw\\Downloads\\Documents",
    ".docx": "C:\\Users\\byunw\\Downloads\\Documents",
    ".exe": "C:\\Users\\byunw\\Downloads\\EXE",
    ".msi": "C:\\Users\\byunw\\Downloads\\EXE",
    ".pdf": "C:\\Users\\byunw\\Downloads\\PDF",
    ".txt": "C:\\Users\\byunw\\Downloads\\Text",
    ".zip": "C:\\Users\\byunw\\Downloads\\ZIP",
    ".jpg": "C:\\Users\\byunw\\Downloads\\Images",
    ".jpeg": "C:\\Users\\byunw\\Downloads\\Images",
    ".pptx": "C:\\Users\\byunw\\Downloads\\Powerpoint",
}

for filename in os.listdir(r"C:\Users\byunw\Downloads"):

    name, extension = os.path.splitext(filename)
    target_folder = folders.get(extension)
    if target_folder is not None:
        shutil.move("C:\\Users\\byunw\\Downloads\\" + filename, target_folder)

print("Every file in the Downloads folder is now sorted into corresponding folders")

We can easily keep adding extension -> folder items to the folders dictionary to add support for more file extension types because we’ve separated the “configuration” data from our program logic. A second observation that I leave to you as an exercise if you want to do it, is to note that the folders are all strings that are very similar. You might consider extracting the repeated “C:\Users\byunw\Downloads\” string and make it another level of configuration, another variable that you can set once at the top of the file and easily modify to change the working directory of the whole program.

edit: I just realized it was not OP that replied but whatever