python method to remove illegal characters when creating a file

  • 2021-01-14 06:04:06
  • OfStack

windows file name cannot contain \ / : * ? " < > | you want to create to filter out these characters


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

Use regularization to remove illegal characters.


Related articles: