python中对文件内容多行内容进行删除

该博客介绍了一种使用Python删除文件内特定多行内容的技巧。通过创建一个临时文件,将待删除的字符串写入,然后比较原文件和临时文件,对不匹配的行保留在新文件中,最后将新文件内容覆盖原文件,从而实现内容删除。
摘要由CSDN通过智能技术生成
# Author Richard_Kong
# !/usr/bin/env python
# --*-- encoding:utf-8 --*--
"""
思路:将要删除的Str保存为新的文件,两个文件对内容比较后进行删除
"""
def delete_file(file,Str):
    '''
    :param file:
    :param Str:
    :return:
    '''

    file_new = file+"_new"
    mark = 0
    file_data = ""
    with open(file_new, mode='w', encoding="utf-8") as f_new:
        f_new.writelines(Str)

    with open(file_new, mode='r+', encoding="utf-8") as f_new:
        with open(file, mode='r+', encoding="utf-8") as f:
                for line in f:
                    mark =0
                    print("line>>>:",line)
                    f_new.seek(0)
                    for str in f_new:
                        print("str>>>:",str)
                        if str in line:
                            print("hjfbv
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值