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

87 views Asked by At

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?

3

There are 3 answers

0
Saddy On BEST ANSWER

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)
0
Axsor On

I made something based off of that, but it says unresolved reference 'file'

import shutil

import os


source = os.listdir("/Users/plapl/Downloads/")

destination1 = "/Users/plapl/desktop/Shortcuts/images"

destination2 = "/Users/plapl/Shortcuts/winrar files"

destination3 = "/Users/plapl/torrents"

for files in source:
    if file.endswith(".png"):
        shutil.move(files, destination1)

    if file.endswith(".zip"):
        shutil.move(files, destination2)

    if file.endswith(".torrent"):
        shutil.move(files, destination3)

2
Prethi G On

It is complaining for the variable name file which is not defined.

You should use files since that is your iterating variable name.