关于python:Python读写文件脚本

该脚本依据输出的门路,能够读取门路下的所有文件,实现匹配字符串替换,增加内容和删除内容的性能。

import os
from fileinput import FileInput

#删除内容
def match_then_delete(inputpath):
    for root,dirs,files in os.walk(inputpath):
        for file in files:
            path = os.path.join(root,file)
            output_file_path = ""+file
            print(out_file_path)
            with open(path,'r',encoding='gbk') as infile:
                input_stream=infile.read()
                output_stream=""
                #换行分切分内容
                input_stream_lines=input_stream.split("\n")
                for line in input_stream_lines:
                    if line.startwith(""):
                        pass
                    else:
                        output_stream=output_stream+line+'\n'
                #读取去掉指定内容后的新内容,从新写文件
                g = open(output_file_path,'w')
                g.write(output_stream)

#增加内容,在匹配内容match上方增加内容content
def match_then_insert(filename,match,content):
    for line in FileInput(filename,inplace=True):
        if match in line:
            line = content+'\n'+line
        print(line,end='')

#匹配字符串替换
def match_then_replace(filename,oldtext,newtext):
    for line in FileInput(filename, inplace=True):
        if oldtext in line:
            line = line.replace(oldtext,newtext)
        print(line,end='')

if __name__=='__main__':
    inputpath = ""
    for root,dirs,files in os.walk(inputpath):
        for file in files:
            path = os.path.join(root,file)
            output_file_path = inputpath+file
            match_then_replace(output_file_path,"oldtext","newtext")

须要留神的点:
当咱们须要解决的文件是utf-8编码时,而python3中默认的文件解码格局是gbk,若间接应用FileInput模块,会报谬误
UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x89 in position 116: illegal multibyte sequence
若咱们应用如下模式

for line in fileinput.input(filename,openhook=fileinput.hook_encoded('utf-8','')

应用openhook指定编码格局为utf-8时,此时则无奈设置inplace=True,即无奈写入文件
这里应用的解决办法是批改fileinput的源码,在340和360行左近,在代码中退出enconding=”utf-8″

参考文章链接:https://www.cnblogs.com/bj-xy/p/6340256.html

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理