# 需求

changeLine

现在的需求是想把-ns ":" 中的冒号替换为-rfn中双引号的内容。

import os
import re


def traverseFolder(targetFolder):
    if(targetFolder[-1]==os.path.sep):
        targetFolder=targetFolder[0:-1]
    for root, dirs, files in os.walk(targetFolder):
        for oneFile in files:
            changeFile(root+os.path.sep+oneFile)

def changeFile(filePath):
    fileSplitName=os.path.splitext(filePath)
    if(fileSplitName[1]==".ma"):
        print("Processing with file:    "+filePath)
        with open(filePath,"r") as fin:
            fileContent=fin.readlines()
            lineNum=0
            fileMaxLineNum=len(fileContent)
            for i in range(0,fileMaxLineNum):
                if re.match(r"file -rdi 1 -ns \":\" -rfn ",fileContent[i]):
                    #result = re.search(r"file -rdi 1 -ns \":\" -rfn \"(.*?)\"",fileContent[i])
                    #print("result:"+result.group(1))
                    fileContent[i]=re.sub(r"file -rdi 1 -ns \":\" -rfn \"(.*?)\"",lambda m:"file -rdi 1 -ns \""+m.group(1).replace("RN","")+"\" -rfn \""+m.group(1)+"\"",fileContent[i])
            with open(filePath,"w") as fout:
                fout.writelines(fileContent)

            #input("输入任意键!")


targetFolder=input("请输入一个目录:")
traverseFolder(targetFolder)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33