Python实现文件文本内容替换

#!/usr/bin/python
# coding=utf-8

import re
import os
s = os.sep
root = os.getcwd()
for root,dirs,files in os.walk(root):
    for name in files:
        if name.endswith(".java"):
            print(os.path.join(root, name))
            a= open(os.path.join(root, name),'r') #打开所有文件
            str = a.read()
            str = str.replace('sdfsdf','sdf')
            b = open(os.path.join(root, name),'w')
            b.write(str) #再写入
            b.close() #关闭文件

你可能感兴趣的:(Python实现文件文本内容替换)