我正在尝试在 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新手,在问这个问题之前我搜索了很长时间,所有关于规范化文件名的提示都没有帮助。
输入后我将脚本从blrp更改为:
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_Match 对象; 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
我希望这会有所帮助。
关于python - AttributeError : 'NoneType' object has no attribute 'group' while using re. 匹配文件名重命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47217846/