替换不能用于文件名的字符

1
2
3
4
5
6
7
8
9
10
import re

def validateTitle(title):
title = title.replace("\r","")
title = title.replace("\n","")
rstr = r"[\/\\\:\*\?\"\<\>\|]" # '/ \ : * ? " < > |'
new_title = re.sub(rstr, "_", title) # 替换为下划线
return new_title

print(validateTitle('<测试*文件名>.txt'))