我在文件夹中有以下示例格式的文件:
2020-02-4–1-11-17131-SERVER-4-10.1.1.1.txt
我想将文件夹中具有此格式的所有文件重命名为以下(从文件末尾删除 ip 地址:
2020-02-4–1-11-17131-SERVER-4.txt
我想使用 python 来做到这一点。
有几种方法可以做到这一点。下面的解决方案是一个相对强大的解决方案,它在文件匹配模式时更新文件,如果不匹配则将它们单独放置:
import re
from pathlib import Path
pattern = re.compile('(.+)-(\d+\.){4}txt')
for file in Path('/path/to/folder').glob('*.txt'):
m = pattern.match(file.name)
if m:
new_file = file.with_name(m.group(1) + ".txt")
file.rename(new_file)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句