关于python:Python读写文件脚本

10次阅读

共计 1491 个字符,预计需要花费 4 分钟才能阅读完成。

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

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

正文完
 0