fork(1) download
  1. import string
  2.  
  3. def format_filename(s):
  4. ## from https://g...content-available-to-author-only...b.com/seanh/93666
  5. """Take a string and return a valid filename constructed from the string.
  6. Uses a whitelist approach: any characters not present in valid_chars are
  7. removed. Also spaces are replaced with underscores.
  8.  
  9. Note: this method may produce invalid filenames such as ``, `.` or `..`
  10. When I use this method I prepend a date string like '2009_01_15_19_46_32_'
  11. and append a file extension like '.txt', so I avoid the potential of using
  12. an invalid filename.
  13.  
  14. """
  15. valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
  16. filename = ''.join(c for c in s if c in valid_chars)
  17. filename = filename.replace(' ','_') # I don't like spaces in filenames.
  18. return filename
  19.  
  20.  
  21. astr='\gbahsd:njs?<>|"asd/as*'
  22. res = format_filename(astr)
  23. print(res)
  24.  
Success #stdin #stdout 0.02s 27768KB
stdin
Standard input is empty
stdout
gbahsdnjsasdas