Created
October 31, 2021 08:56
-
-
Save xhlove/5c3c395545e4f5c50cfba69945239e25 to your computer and use it in GitHub Desktop.
去除非法字符,确保文件名正常,文件可以正常写入
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fix_name(name: str): | |
''' | |
去除可能存在的非法字符 | |
''' | |
exclude_chars = ["\\", "/", ":", ":", "*", "?", "\"", "<", ">", "|", "\r", "\n", "\t"] | |
# 去除非法符号 | |
name = ''.join(char if char not in exclude_chars else ' ' for char in name) | |
# 将之前连续的非法符号 改为单个 _ | |
return '_'.join(name.split()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment