通过在当前名称中添加短划线来重命名文件

时间:2016-10-01 14:25:53

标签: python regex rename

我目前有一个按许可证号命名的文件列表。

需要通过添加五个短划线来重命名它们。

示例:

988003077M02471.pdf <--old
9-88-003-07-7M-02471.pdf <--new

尝试在Python中编写一个正则表达式,它将获取缺少破折号的文件名并添加破折号。 regex101.com生成了这段代码。

importre

regex = r"(.)(..)(...)(..)(..)(.*)"

test_str = "988003077M02471.pdf"

subst = "\\1-\\2-\\3-\\4-\\5-\\6"

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0)

if result:
print (result)

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

当我运行脚本时,它没有添加破折号到文件名。我需要改变什么?

0 个答案:

没有答案
相关问题