Python 属性错误:';非类型';对象没有属性';集团';使用re.match重命名文件名时
我正在尝试在windows上使用Pyton重命名文件夹中以相同字符串(Vertragshandbuch_Beitrag_)开头的一些文件 示例文件名: Vertragshandbuch_Beitrag_004_条款清单.docx 新文件名应如下所示:4.docx 我当前的代码如下所示:Python 属性错误:';非类型';对象没有属性';集团';使用re.match重命名文件名时,python,regex,Python,Regex,我正在尝试在windows上使用Pyton重命名文件夹中以相同字符串(Vertragshandbuch_Beitrag_)开头的一些文件 示例文件名: Vertragshandbuch_Beitrag_004_条款清单.docx 新文件名应如下所示:4.docx 我当前的代码如下所示: import os import re for filename in os.listdir("."): m = re.match("Vertragshandbuch_Beitrag_(\d+)_(\w
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.组(1)
AttributeError:“非类型”对象没有属性“组”
我在这里用几个文件名检查了正则表达式:它总是完美匹配的
我是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)
我得到了以下结果:
看起来它是匹配的,仍然是错误 调用
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)
我用你提供的名字(编号001-007)和
这是我的输出:
我希望这能有所帮助。如果您愿意,您也可以选择进行匹配。这样,即使字符串不匹配,也会得到一个匹配对象(而不是
None
)
把这个放在上面,因为它非常重要:*
和类似的东西
将运行可选的匹配项,因此如果您不这样做,这将不起作用
了解要捕获的子字符串周围可能存在的内容
话虽如此,这是通常的行为
>>> re.match('(a)', a).groups()
('a',)
>>> re.match('(a)', b).groups()
AttributeError: 'NoneType' object has no attribute 'groups'
括号后的A?
使“A”的匹配成为可选
>>> re.match('(a)?', 'a').groups()
('a',)
>>> re.match('(a)?', 'b').groups()
(None,)
您甚至可以将默认值传递给组
re.match('(a)?', 'a').groups('cannot find a')
('a',)
re.match('(a)?', 'b').groups('cannot find a')
('cannot find a',)
这有时会使代码更具可读性。
re.match()
在正则表达式不匹配时返回None
。如果m不是None,您可以通过执行来检查它。另外,您需要在regex模式前面加上r
的前缀,比如r“xyz”
,否则\d
之类的东西就不起作用了。有关这方面的更多信息,请参阅。我将您的正则表达式(regex)(r'
版本)和示例文件名复制到一个小脚本中,它对我很有用。目录中的所有文件都与模式匹配吗?如果没有,则必须检查None
。我试过:如果m不是None os.rename(filename,new\u filename)print(new\u filename)
在None
之后似乎缺少一个冒号。非常感谢Jebby。现在,它首先在测试文件夹中工作,其中包含一个以前不工作的文件,而不是在对regex进行了一些更改之后在整个文件夹中工作,以获得其中包含“-”的名称。一定是重新编译的结果。谢谢没问题。如果这个答案对您有帮助,请接受它作为解决方案。@renare.compile()
对模式是否匹配没有任何影响。这是正确的re.compile()
只是比re.match()
的速度有所提高,所以在一次将该模式与多个字符串匹配时,它是首选。(就像在for循环中一样)。我应该提到这一点。感谢@BlurpAlso,请注意,当使用诸如re.match()
和re.search()
之类的函数时,模式将自动编译和缓存()。
re.match('(a)?', 'a').groups('cannot find a')
('a',)
re.match('(a)?', 'b').groups('cannot find a')
('cannot find a',)