简体   繁体   English

Python Shutil integer 范围内移动文件的正则表达式

[英]Regular expression in Python Shutil integer range to move files

I have a folder with 12500 pictures.我有一个包含 12500 张图片的文件夹。 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-7999 should be copied to the first folder.范围为 0-7999 的文件应复制到第一个文件夹。 Files 8000-9999 should be copied to the second folder and files with range 10000-12499 should be copied to the third folder.文件 8000-9999 应复制到第二个文件夹,范围为 10000-12499 的文件应复制到第三个文件夹。

First, I thought I could easily use [0-7999].jpg for the first folder, [8000-9999].jpg for the second and [10000-12499].jpg for the third.首先,我认为我可以轻松地将 [0-7999].jpg 用于第一个文件夹,将 [8000-9999].jpg 用于第二个文件夹,将 [10000-12499].jpg 用于第三个文件夹。 However, this does not work.但是,这不起作用。 I figured out the following code, based on the wildcards I know which are?我根据我知道的通配符找出了以下代码? and *: The following code does work and does the job (please note that I commented out the shutil.copy, instead use print to check the result):和 *:以下代码确实有效并且完成了工作(请注意,我注释掉了 shutil.copy,而是使用 print 来检查结果):

import glob
import shutil
dest_dir = "/tmp/folder1/"
for file in glob.glob('/tmp/source/?.jpg'):
    #shutil.copy(file, dest_dir)
    print(file)

dest_dir = "/tmp/folder1/"
for file in glob.glob('/tmp/source/??.jpg'):
    #shutil.copy(file, dest_dir)
    print(file)

dest_dir = "/tmp/folder1/"
for file in glob.glob('/tmp/source/???.jpg'):
    #shutil.copy(file, dest_dir)
    print(file)

dest_dir = "/tmp/folder1/"
for file in glob.glob('/tmp/source/[1-7]???.jpg'):
    #shutil.copy(file, dest_dir)
    print(file)

dest_dir = "/tmp/folder2/"
for file in glob.glob('/tmp/source/[8-9]???.jpg'):
    #shutil.copy(file, dest_dir)
    print(file)

dest_dir = "/tmp/folder3/"
for file in glob.glob('/tmp/source/?????.jpg'):
    #shutil.copy(file, dest_dir)
    print(file)

However, I would like to have an elegant solution for this.但是,我想为此提供一个优雅的解决方案。 I googled regular expression with integer range and tried the following:我用 integer 范围搜索正则表达式并尝试了以下操作:

dest_dir = "/tmp/folder3/"
for file in glob.glob('/tmp/source/\b([0-9]|[1-9][0-9]|[1-9][0-9][0-9]|1000).jpg'):
    #shutil.copy(file, dest_dir)
    print(file)

This does not work.这不起作用。 So how does a correct implementation look like?那么正确的实现是怎样的呢? I need a solution for both, shutil.copy and shutil.move, but I think it is the same for both.我需要一个针对shutil.copy 和shutil.move 的解决方案,但我认为两者都是一样的。

You can get all files ( *.jpg ) and then decide for each file where it should go您可以获取所有文件 ( *.jpg ),然后为每个文件决定它应该 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM