2020 Internet: 10 more reliable forecasts

2023-01-23   ES  

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()

source

Related Posts

mybatis ﹤! [Cdata []]> Use

Three -eyed operator

[Luoya Novice Village] P1980 counting problem Z

Learn Microsoft Power BI series together-Use techniques (2) Connect Excel data source error solution

2020 Internet: 10 more reliable forecasts

Random Posts

PACKAGE Explorer does not report errors, projects and bags and classes do not prompt red x

TextView some use techniques

Thesis reading: FPN

JS how to operate the DOM rolling to the visible area? Dark

bzoj 3252: Raiders greedy tree chain cut BLACKJACK