1. 程式人生 > >python----資料夾中,txt內容的批量替換

python----資料夾中,txt內容的批量替換

#coding=utf-8
import os
def listFiles(dirPath):
    fileList = []
    for root, dirs, files in os.walk(dirPath):
        for fileObj in files:
            fileList.append(os.path.join(root, fileObj))
    return fileList
def main():
    fileDir = "E:\\python\\datatwoweek\\transition"
    regex = ur'FUNC_SYS_ADD_ACCDETAIL'
    fileList = listFiles(fileDir)
    fileList = listFiles(fileDir)
    for fileObj in fileList:
        f = open(fileObj, 'r+')
        all_the_lines = f.readlines()
        f.seek(0)
        f.truncate()
        for line in all_the_lines:
            f.write(line.replace('a', 'b').replace('c', 'd').replace('e', 'f'))
f.close() if __name__ == '__main__': main()