How to move all only one type of file to another directory? For example, i want to move only all .jpg only file to another folder in Python. I know batch is easier to do it, but i prefer in Python. Anyone can help?

in java i use new File(Folder path + "."); to list all files u can try (Folder path + ".jpg")

Python

I would use os.listdir, and shutil.move (or copy). Here's a useful function to sort by extension:

def filterbyext(filelist,ext):
    """Returns all files of type ext in filelist"""
    returnfiles = []
    for item in filelist:
        x = item.split(".")
        try:
            #Take all files of THIS type
            if str(x[1]) == ext:
                returnfiles.append(item)
        except IndexError:
            pass
    return returnfiles

So this is what it would look like:

for jpg in filterbyext(os.listdir(os.getcwd()),'jpg'):
     shutil.move(jpg,newdest)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.