Method of filtering illegal characters in Windows file name in Python

  • 2021-06-28 09:27:36
  • OfStack

There are three ways to write on the web:

Type 1 (all illegal characters are not escaped):


def setFileTitle(self,title):   
      fileName = re.sub('[\/:*?"<>|]','-',title)# Remove illegal characters  
      self.file = open(fileName + ".txt","w+") 

\Illegal characters must be escaped or \/interpreted as/

Type 2 (all illegal characters are escaped):


def validateTitle(title):
  rstr = r"[\/\\\:\*\?\"\<\>\|]" # '/ \ : * ? " < > |'
  new_title = re.sub(rstr, "_", title) #  Replace with underline 
  return new_title

The third (partial illegal character escape):


private static Pattern FilePattern = Pattern.compile("[\\\\/:*?\"<>|]"); 
public static String filenameFilter(String str) { 
  return str==null?null:FilePattern.matcher(str).replaceAll(""); 
} 

Best Writing:


pattern=r'[\\/:*?"<>|\r\n]+'

In [] * does not need escaping, in which case * does not mean multiple matches, it means its own character

Be careful

The string of folder names and file names may contain special characters that are not 26 letters or numbers, such as the string''burgundy'', which can be named successfully. On the other hand, the spaces at the beginning and end of the file name should be removed, the dots at the beginning and end should be removed, and special blank characters such as line breaks and tabs should also be removed.

strip(), lstrip(), and rstrip() in python take out some special characters at both ends.


Related articles: