Python函数使任意string有效的文件名

有没有一个内置的function,从string中去除不能在Windows文件名中的所有字符或以某种方式replace它们?

例如function("Some:unicode\symbols") – > "Some-unicode-symbols"

 import re arbitrary_string = "File!name?.txt" cleaned_up_filename = re.sub(r'[/\\:*?"<>|]', '', arbitrary_string) filepath = os.path.join("/tmp", cleaned_up_filename) with open(filepath, 'wb') as f: # ... 

采取从用户gx
显然适应你的情况。