Python Shutil整数范围中的正则表达式可移动文件 [英] Regular expression in Python Shutil integer range to move files

查看:186
本文介绍了Python Shutil整数范围中的正则表达式可移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含12500张图片的文件夹.文件名包含数字,因此看起来像:

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

现在我要移动文件.范围为0-7999的文件应复制到第一个文件夹.应该将文件8000-9999复制到第二个文件夹,将文件范围10000-12499的文件复制到第三个文件夹.

Now I want to move the files. Files with range 0-7999 should be copied to the first folder. Files 8000-9999 should be copied to the second folder and files with range 10000-12499 should be copied to the third folder.

首先,我想我可以轻松地将[0-7999] .jpg用于第一个文件夹,将[8000-9999] .jpg用于第二个文件夹,将[10000-12499] .jpg用于第三个文件夹.但是,这不起作用.我根据我知道的通配符找出了以下代码?和 *: 下面的代码确实可以工作,并且可以完成工作(请注意,我已注释掉shutil.copy,而使用print来检查结果):

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. 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):

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:

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)

这不起作用.那么正确的实现是什么样的呢?我需要同时针对shutil.copy和shutil.move的解决方案,但我认为两者都是相同的.

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.

推荐答案

您可以获取所有文件(*.jpg),然后为每个文件确定应存放的位置

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

这篇关于Python Shutil整数范围中的正则表达式可移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆