Skip to content

Instantly share code, notes, and snippets.

@xhlove
Created October 31, 2021 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xhlove/5c3c395545e4f5c50cfba69945239e25 to your computer and use it in GitHub Desktop.
Save xhlove/5c3c395545e4f5c50cfba69945239e25 to your computer and use it in GitHub Desktop.
去除非法字符,确保文件名正常,文件可以正常写入
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