What is the most cross platform way of removing bad path characters (e.g. "\" or ":" on Windows) in Python?
Because there seems to be no ideal solution I decided to be relatively restrictive and did use the following code:
def remove(value, deletechars): for c in deletechars: value = value.replace(c,'') return value; print remove(filename, '\/:*?"<>|')
You can simply use C# inbuilt function " Path. GetInvalidFileNameChars() " to check if there is invalid character in file name and remove it.
os. remove() method in Python is used to remove or delete a file path. This method can not remove or delete a directory. If the specified path is a directory then OSError will be raised by the method.
I think the safest approach here is to just replace any suspicious characters. So, I think you can just replace (or get rid of) anything that isn't alphanumeric, -, _, a space, or a period. And here's how you do that:
import re re.sub(r'[^\w\-_\. ]', '_', filename)
The above escapes every character that's not a letter, '_'
, '-'
, '.'
or space with an '_'
. So, if you're looking at an entire path, you'll want to throw os.sep in the list of approved characters as well.
Here's some sample output:
In [27]: re.sub(r'[^\w\-_\. ]', '_', r'some\*-file._n\\ame') Out[27]: 'some__-file._n__ame'
Unfortunately, the set of acceptable characters varies by OS and by filesystem.
Windows:
- Use almost any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:
- The following reserved characters are not allowed:
< > : " / \ | ? *- Characters whose integer representations are in the range from zero through 31 are not allowed.
- Any other character that the target file system does not allow.
The list of accepted characters can vary depending on the OS and locale of the machine that first formatted the filesystem.
.NET has GetInvalidFileNameChars and GetInvalidPathChars, but I don't know how to call those from Python.
Your best bet is probably to either be overly-conservative on all platforms, or to just try creating the file name and handle errors.
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