Regular expression Re module finds the ASCII code with Findall, so the corresponding ASCII code is required to match the successful match when replacing the replacement. The following program is to find the file name of the file under the folder, and replace the man to 1, and replace the woman to 2 to 2
# -*- coding: utf-8 -*-
import fnmatch
import os
import codecs
import re
import sys
def iterfindfiles(path, fnexp):
for root, dirs, files in os.walk(path):
for filename in fnmatch.filter(files, fnexp):
yield os.path.join(root, filename)
def fiterFiles():
path=raw_input("input dir:")
filterfiletype=raw_input("input file filter type:")
#quanjiao_2_banjiao(path)
for filename in iterfindfiles(path,filterfiletype):
SingerList=re.findall('\((..?)\)\.dat',filename)
for SingerName in SingerList:
if(SingerName=='\xc4\xd0'):
SingerName='1'
new_filename=re.sub('\((..?)\)\.dat','(1).dat',filename)
print new_filename
os.rename(filename,new_filename)
#print SingerName
elif(SingerName=='\xc5\xae'):
SingerName='2'
new_filename=re.sub('\((..?)\)\.dat','(2).dat',filename)
print new_filename
os.rename(filename,new_filename)
def main():
fiterFiles()
if __name__ == "__main__":
main()