pythonfile-organization

How do I move file types not a specific file in PyCharm?


I am trying to make a program to organize my downloads folder every time I download something, but if I use this code:

import shutil

shutil.move("/Users/plapl/downloads/.zip", "/Users/plapl/Desktop/Shortcuts/winrar files")
shutil.move("/Users/plapl/downloads/.png", "/Users/plapl/Desktop/Shortcuts/images")

It searches for a file name called .zip and .png, but I want it to search for all files that are that type. Can anyone tell me how to do that?


Solution

  • You want to iterate over the files in the directory. Here is an example from source

    import shutil
    import os
    source = os.listdir("/Users/plapl/downloads/")
    destination = "/Users/plapl/Desktop/Shortcuts/winrar files"
    for files in source:
        if files.endswith(".zip"):
            shutil.move(files,destination)