# 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
python中对文件内容多行内容进行删除
最新推荐文章于 2024-08-30 11:26:48 发布
该博客介绍了一种使用Python删除文件内特定多行内容的技巧。通过创建一个临时文件,将待删除的字符串写入,然后比较原文件和临时文件,对不匹配的行保留在新文件中,最后将新文件内容覆盖原文件,从而实现内容删除。
摘要由CSDN通过智能技术生成