I have a string which contain some data I parse from the web, and make a file named after this data.
string = urllib.urlopen("http://example.com").read()
f = open(path + "/" + string + ".txt")
f.write("abcdefg")
f.close()
The problem is that it may include one of this characters: \ / * ? : " < > |
.
I'm using Windows, and it is forbidden to use those characters in a filename.
Also, string
is in Unicode formar which makes most of the solutions useless.
So, my question is: what is the most efficient / pythonic way to strip those characters? Thanks in advance!
Edit: the filename is in Unicode format not str!
You can simply use C# inbuilt function " Path. GetInvalidFileNameChars() " to check if there is invalid character in file name and remove it. var InvalidCharacters= Path. GetInvalidFileNameChars(); string GetInvalidCharactersRemovedString= new string(fileName .
You can name files using almost any character for a name, except for the following reserved characters: < > : " / \ | ? * The maximum length for a path is 255 characters. This limitation includes the drive letter, colon, backslash, directories, subdirectories, filename, and extension.
we dont know how your data look like:
But you can use re.sub
:
import re
your_string = re.sub(r'[\\/*?:"<>|]',"","your_string")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With