Python批量替换指定文件夹中文件内容

脚本说明:批量替换指定文件夹下文件内容

#coding:utf-8
import os
import re

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 = "You filedir"
    regex = ur'FUNC_SYS_ADD_ACCDETAIL'
    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:
            str1 = 'original string'
            str2 = 'replaced string'
	    f.write(line.replace(str1 , str2))    
	f.close()  

if __name__=='__main__':
    main() 


你可能感兴趣的:(Python)