1

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 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!

Kami
  • 241
  • 4
  • 12
  • 2
    http://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename-in-python is a very similar question. take a look – dting Mar 26 '11 at 19:53

3 Answers3

8
>>> import re
>>> print re.sub(r'[\\/:"*?<>|]+', "", "This\ Ăwesomé_Song?©.mp3")
This Ăwesomé_Song©.mp3
>>> 
dting
  • 38,604
  • 10
  • 95
  • 114
  • I was able to easily adapt this solution for Groovy (and handle few extra characters for my own needs): filename = filename.replaceAll(~/[\\/:"*?<>| #,]+/, '-') – kjl May 23 '12 at 22:28
1

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.

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
0

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.

Community
  • 1
  • 1
ikostia
  • 7,395
  • 6
  • 30
  • 39