Regular expression in Python Shutil integer range to move files

后端 未结 1 360
野的像风
野的像风 2021-01-28 00:35

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 mo

相关标签:
1条回答
  • 2021-01-28 01:11

    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
    
    0 讨论(0)
提交回复
热议问题