Skip to content Skip to sidebar Skip to footer

Regular Expression In Python Shutil Integer Range To Move Files

I have a folder with 12500 pictures. The filenames contain the numbers, so it looks like: 0.jpg 1.jpg 2.jpg 3.jpg . . .12499.jpg Now I want to move the files. Files with range 0-7

Solution 1:

You can get all files (*.jpg) and then decide for each file where it should go

import glob
import shutil
import os

dest_dirs = {0:"/tmp/folder1/", 8000:"/tmp/folder2/", 10000:"/tmp/folder3/"}
for file in glob.glob('*.jpg'):
    base = os.path.basename(file)  # remove path
    withoutext = os.path.splitext(base)[0]  # remove extension
    try:
        number = int(withoutext)
        for key, value in dest_dirs.items():
            if number >= key:
                destination = value
        # shutil.copy(file, os.path.join(destination, base))
        print(file, os.path.join(destination, base))
    except ValueError:
        # file name is not a number
        pass

Post a Comment for "Regular Expression In Python Shutil Integer Range To Move Files"