简体   繁体   English

根据文件名分隔文件

[英]Segregate files based on filename

I've got a directory containing multiple images, and I need to separate them into two folders based on a portion of the file name. 我有一个包含多个图像的目录,我需要根据文件名的一部分将它们分成两个文件夹。 Here's a sample of the file names: 这是文件名的示例:

  1. 22DEC16 7603 520981127600_03.jpg 16DEC16 7603 520981127600_03.jpg
  2. 13NOV16 2302 999230157801_07.jpg 13NOV16 2302 999230157801_07.jpg
  3. 08JAN14 7603 811108236510_02.jpg 08JAN14 7603 811108236510_02.jpg
  4. 21OCT15 2302 197661710099_07.jpg 21OCT15 2302 197661710099_07.jpg
  5. 07MAR17 2302 551529900521_01.jpg 2017年3月7日2302 551529900521_01.jpg
  6. 19FEB17 3211 074174309177_09.jpg 19FEB17 3211 074174309177_09.jpg
  7. 19FEB17 3211 881209232440_02.jpg 19FEB17 3211 881209232440_02.jpg
  8. 19FEB17 2302 491000265198_04.jpg 19FEB17 2302 491000265198_04.jpg

I need to move the files into two folders according to the numbers in bold after the date - so files containing 2302 and 3211 would go into an existing folder named "panchromatic" and files with 7603 would go into another folder named "sepia". 我需要根据日期后以粗体显示的数字将文件移动到两个文件夹中-因此包含2302和3211的文件将进入一个名为“全色”的现有文件夹,而包含7603的文件将进入另一个名为“ sepia”的文件夹。

I've tried multiple examples from other questions, and none seem to fit this problem. 我从其他问题中尝试了多个示例,但似乎没有一个适合这个问题。 I'm very new to Python, so I'm not sure what example to post. 我是Python的新手,所以不确定要发布哪个示例。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Without giving you the solution, here's what I'd recommend. 在没有给您解决方案的情况下,这就是我的建议。

  1. Use os.listdir to iterate over files in your directory. 使用os.listdir遍历目录中的文件。

     path = '/path/to/dir/' for file in os.listdir(path): ... 
  2. Check the 4 digits by slicing your string. 通过对字符串进行切片来检查4位数字。 By the looks of it, you'd need to get file[6:10] 从外观上看,您需要获取file[6:10]

  3. Check if int(file[6:10]) in {2302, 2311} . 检查if int(file[6:10]) in {2302, 2311} If yes, dst = /path/to/panchromatic . 如果是,则dst = /path/to/panchromatic Else, dst = /path/to/sepia/ 否则, dst = /path/to/sepia/

  4. Use shutil.move to move files. 使用shutil.move移动文件。 Something like shutil.move(os.path.join(path, file), dst) , where os.path.join joins path artefacts. 诸如shutil.move(os.path.join(path, file), dst) ,其中os.path.join连接路径伪像。

  5. Make sure you import os and import shutil at the top of your script. 确保在脚本顶部import osimport shutil

You can do this the easy way or the hard way. 您可以通过简单或困难的方式进行操作。

Easy way 简单的方法

Test if your filename contains the substring you're looking for. 测试文件名是否包含要查找的子字符串。

import os
import shutil
files = os.listdir('.')
for f in files:
    # skip non-jpeg files
    if not f.endswith('.jpg'):
        continue
    # move if panchromatic
    if '2302' in f or '3211' in f:
        shutil.move(f, os.path.join('panchromatic', f))
    # move if sepia
    elif '7603' in f:
        shutil.move(f, os.path.join('sepia', f))
    # notify if something else
    else:
        print('Could not categorize file with name %s' % f)

This solution in its current form is susceptible to mis-classification, as the number we're looking for can appear by chance later in the string. 当前形式的此解决方案易于分类错误,因为我们正在寻找的数字可能会在字符串的后面偶然出现。 I'll leave you to find ways to mitigate this. 我将让您找到减轻这种情况的方法。

Hard way 艰辛的道路

Regular expressions. 常用表达。 Match the four letter digits after the date with a regular expression. 将日期后的四个字母数字与正则表达式匹配。 Left for you to explore! 留给您探索!

Self explanative, with Python 3, or Python 2 + backport pathlib : 使用Python 3或Python 2 + backport pathlib自我说明:

import pathlib
import shutil

# Directory paths. Tailor this to your files layout
# see https://docs.python.org/3/library/pathlib.html#module-pathlib
source_dir = pathlib.Path('.')
sepia_dir = source_dir / 'sepia'
panchro_dir = source_dir / 'panchromatic'

assert sepia_dir.is_dir()
assert panchro_dir.is_dir()

destinations = {
    ('2302', '3211'): panchro_dir,
    ('7603',): sepia_dir
}

for filename in source_dir.glob('*.jpg'):
    marker = str(filename)[7:11]
    for key, value in destinations.items():
        if marker in key:
            filepath = source_dir / filename
            shutil.move(str(filepath), str(value))

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

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