Python function to make arbitrary strings valid filenames
是否有一个内置函数从字符串中删除Windows文件名中不能包含的所有字符或以某种方式替换这些字符?
例如:
1 2 3 4 5 6 7 8 | 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
显然要适应你的情况。