Make string a valid file name that can later be viewed as original string

Question:

Say I have a string

"this is | test"

and I want to use that string as a file name but it is not valid to have the | character inside a file name. What is the best way to replace all characters that are not valid as a file name with characters that are valid but later on I can read the saved file name by replacing back all the prior replaced invalid characters?

To explain better say the initial string was

this is | test

then I replaced the string to "this is # test" and saved the file now I can re-read that file name as the original string by just replacing the # with |. What is the best way to achieve this for all invalid strings?

Asked By: seriously

||

Answers:

You can use URL quoting as URL-safe characters are generally safe for filenames too:

>>> import urllib.parse
>>> urllib.parse.quote("this is | test", safe=" ")
'this is %7C test'
>>> urllib.parse.unquote("this is %7C test")
'this is | test'

or you can base64 encode/decode your string:

>>> import base64
>>> base64.b64encode("this is | test".encode())
b'dGhpcyBpcyB8IHRlc3Q='
>>> base64.b64decode(b'dGhpcyBpcyB8IHRlc3Q=').decode()
'this is | test'
Answered By: Selcuk
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.