Skip to main content
 首页 » 编程设计

python之AttributeError : 'NoneType' object has no attribute 'group' while using re. 文件名重命名匹配

2024年10月01日8exmyth

我正在尝试在 Windows 上使用 pyhton 重命名文件夹中以相同字符串 (Vertragshandbuch_Beitrag_) 开头的一些文件。

示例文件名: Vertragshandbuch_Beitrag_004_Term Sheet.docx

新文件名应该是这样的:4.docx

我当前的代码如下所示:

import os 
import re 
 
for filename in os.listdir("."): 
    m = re.match("Vertragshandbuch_Beitrag_(\d+)_(\w+(\W\w+)*)\.docx", filename)     
    number = m.group(1)   
    new_filename = number + ".docx" 
    os.rename(filename, new_filename) 
    print(new_filename) 

我收到这个错误: 追溯(最近一次通话): 文件“C:(...)rename.py”,第 6 行,位于 数 = m.group(1) AttributeError: 'NoneType' 对象没有属性 'group'

我在这里用几个文件名检查了正则表达式:https://regex101.com/它始终是完美的匹配。

我是 python 的新手,在问这个问题之前我搜索了很长时间,所有关于规范化文件名的提示都没有帮助。

我将输入后的脚本从 blurp 更改为:

import os 
import re 
 
for filename in os.listdir("."): 
    m = re.match(r'Vertragshandbuch_Beitrag_(\d+)_(\w+(\W\w+)*)\.docx', filename)     
    number = m.group(1)   
    new_filename = number + ".docx" 
    os.rename(filename, new_filename) 
    print(new_filename) 

当我检查正则表达式时仍然出现相同的错误并且仍然匹配。

测试我现在使用的正则表达式匹配:

import os 
import re 
 
for filename in os.listdir("."): 
    m = re.match(r'Vertragshandbuch_Beitrag_(\d+)_(\w+(\W\w+)*)\.docx', filename)   
    number = m.group(1)   
    new_filename = number + ".docx" 
    if m is not None: 
        os.rename(filename, new_filename) 
        print(new_filename) 

还是一样的错误信息。

好吧,作为最后的手段,我在一个只包含文件 Vertragshandbuch_Beitrag_003_Letter.docx 的文件夹中尝试了这个:

import os, sys 
import re 
 
for filename in os.listdir("."): 
    m = re.match(r"Vertragshandbuch_Beitrag_(\d+)_(\w+(\W\w+)*)\.docx", filename)     
    print(m) 

我得到了以下结果: <_sre.SRE_匹配对象; span=(0, 40), match='Vertragshandbuch_Beitrag_003_Letter.docx'>

看起来是匹配的,还是报错。

请您参考如下方法:

当您调用 re.match() 时,如果提供的字符串与正则表达式模式不匹配,它将等于 None。

我假设的问题是,您遇到的文件名与您提供的正则表达式模式不匹配。

即使正则表达式正确匹配您的文件,第一次 re.match() 返回 None 时它也会中断,除非您明确捕获它。否则,当您调用 re.match().group() 时,它不存在并引发错误。

当我使用指定的名称格式制作文件时,这对我有用:

import os 
import re 
 
def rename_num(path): 
 
    # Create a pattern to match filenames to 
    match_pattern = r"Vertragshandbuch_Beitrag_(\d+)_(\w+(\W\w+)*)\.docx" 
    pattern = re.compile(match_pattern) 
 
 
    # For each file in the path supplied above 
    for filename in os.listdir(path): 
 
        # Use the re module to match the regex pattern to the filename. 
        # If the filename doesn't match the regex found will be equal to None. 
        found = pattern.match(filename) 
 
        # If found is not equal to None, print the filename, groups and rename the file 
        if found: 
 
            os.rename(os.path.join(path, filename), os.path.join(path, found.group(1) + ".docx")) 
 
            print("{} renamed to {}".format(filename, found.group(1) + ".docx")) 
 
 
 
# To run the above method in the directory the script is in: 
p = os.path.abspath(os.path.dirname(__file__)) 
rename_num(p) 

I created files with names like you supplied (numbers 001 - 007) and

this was my output:

Vertragshandbuch_Beitrag_001_Term Sheet.docx renamed to 001.docx 
Vertragshandbuch_Beitrag_002_Term Sheet.docx renamed to 002.docx 
Vertragshandbuch_Beitrag_003_Term Sheet.docx renamed to 003.docx 
Vertragshandbuch_Beitrag_004_Term Sheet.docx renamed to 004.docx 
Vertragshandbuch_Beitrag_005_Term Sheet.docx renamed to 005.docx 
Vertragshandbuch_Beitrag_006_Term Sheet.docx renamed to 006.docx 
Vertragshandbuch_Beitrag_007_Term Sheet.docx renamed to 007.docx 

希望对您有所帮助。