I would like to save a file with the name that has the most valid characters remaining intact, f.e. if I get supplied the filename:
>This\ Ăwesomé_Song?©.mp3
and I want to save it under Windows 7
, it won't let me save it until I remove >
, \
and ?
. The characters Ă
, ©
and é
are totally fine and I would like to keep them intact (instead of f.e. just running everything through an ASCII
filter).
I don't know which characters are allowed f.e. under Unix
and the like but I would like it to work platform-independently. The way I would solve this problem is by implementing a list of 开发者_JAVA技巧strings that each contain a class of characters, ranked from most vicious (f.e. ?
) to most harmless (f.e. the letter a
) and knocking them out string by string until I get a filename that i can save.
Language is Python
, thanks!
>>> import re
>>> print re.sub(r'[\\/:"*?<>|]+', "", "This\ Ăwesomé_Song?©.mp3")
This Ăwesomé_Song©.mp3
>>>
In posix, only two octets are reserved, forward slash (/
, 0x2F, dec 42), and null (\0
, 0x00, dec 0). Any other character could appear in a filename.
You may use this pattern from this question to list all valid characters:
>>> import string
>>> valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
And then just check your filename manually.
精彩评论