사용자 제공 입력을 파일 이름으로 변환해야하는 IRC 및 XMPP 봇을 프로그래밍 중입니다. 나는 이것을하기위한 함수를 이미 작성했다. 충분히 맑은가요?Python의 userinput에서 파일 이름 저장하기
allowednamechars = string.ascii_letters + string.digits + '_+/$.-'
def stripname(name, allowed=""):
""" strip all not allowed chars from name. """
n = name.replace(os.sep, '+')
n = n.replace("@", '+')
n = n.replace("#", '-')
n = n.replace("!", '.')
res = u""
for c in n:
if ord(c) < 31: continue
elif c in allowednamechars + allowed: res += c
else: res += "-" + str(ord(c))
return res
그것은 파일 이름 Google App Engine 호환되도록 별도의 제어 문자를 제거하고 os.sep 대체하는 코드뿐만 아니라 일부 repaces와 화이트리스트가있다 : 여기
는 코드입니다.문제의 로봇은 http://jsonbot.googlecode.com입니다.
그래서 어떻게 생각하십니까?
? 항상 올바른 디렉토리에 저장하십시오. 또한 파일이 이미 있으면 어떻게됩니까? – Patrick
특히, Windows에서'os.sep'는 \이지만/또한 구분 기호로 사용됩니다. –
변환의 요점은 무엇입니까? 독창성이 필요합니까? 검색 가능성? 코드에서 충돌이 발생할 수 있으며 피할 것인지 여부는 분명하지 않습니다. –