python3对同一路径下所有文件的指定内容进行替换修改并添加(源码无BUG可直接运行)

本文介绍了一种在Python中批量处理文件的方法,可以删除、替换或在保留原行的基础上替换文件中特定关键字,适用于批量更新配置文件或源代码。

将目录中文件带有‘before’关键字的整行删除

变化前
abcdefg
**<before>**
hijklmn
**<before>**

变化后
abcdefg
hijklmn
import os
import copy
path = input("请输入文件所在路径:")
while 0 == os.path.exists(path):
    print("路径不存在,请输入正确的路径!")
    path = input("请输入文件所在路径:")
before = input("请输入要删除的行包含的关键字:")
files = os.listdir(path)
count = 0
for file in files:
    if os.path.isfile(path+"/"+file):
        fp = open(path+"/"+file,'r')
        lines = []
        for line in fp:
            lines.append(line)

        s=[s for s in lines if before in s]
        if s:
            for i in range(len(s)):
                lines.pop(lines.index(s[i]))
            fp = open(path+"/"+file,'w')
            fp.writelines(lines)
            print(file + " is ok!")
            count += 1
        fp.close()
if 0 == count:
    print("没有文件匹配!")

将目录中文件带有‘before’关键字的替换成‘after’

变化前
abcdefg
**<before>**
hijklmn
**<before>**

变化后
abcdefg
**<after>**
hijklmn
**<after>**
import os
import copy
path = input("请输入文件所在路径:")
while 0 == os.path.exists(path):
    print("路径不存在,请输入正确的路径!")
    path = input("请输入文件所在路径:")
before = input("请输入替换前的关键字:")
after = input("请输入替换后的关键字:")
files = os.listdir(path)
count = 0
for file in files:
    if os.path.isfile(path+"/"+file):
        fp = open(path+"/"+file,'r')
        lines = []
        for line in fp:
            lines.append(line)

        s=[s for s in lines if before in s]
        if s:
            for i in range(len(s)):
                temp=lines[lines.index(s[i])]
                temp=temp.replace(before,after)
                lines.insert(lines.index(s[i])+1,temp)
                lines.pop(lines.index(s[i]))
            fp = open(path+"/"+file,'w')
            fp.writelines(lines)
            print(file + " is ok!")
            count += 1
        fp.close()
if 0 == count:
    print("没有文件匹配!")

将目录中文件带有‘before’关键字的替换成‘after’,保留原来的行添加到下一行

变化前
abcdefg
**<before>**
hijklmn
**<before>**

变化后
abcdefg
**<before>**
**<after>**
hijklmn
**<before>**
**<after>**
import os
import copy
path = input("请输入文件所在路径:")
while 0 == os.path.exists(path):
    print("路径不存在,请输入正确的路径!")
    path = input("请输入文件所在路径:")
before = input("请输入替换前的关键字:")
after = input("请输入替换后的关键字:")
files = os.listdir(path)
count = 0
for file in files:
    if os.path.isfile(path+"/"+file):
        fp = open(path+"/"+file,'r')
        lines = []
        for line in fp:
            lines.append(line)
        replacelines = copy.deepcopy(lines)

        s=[s for s in lines if before in s]
        if s:
            for i in range(len(s)):
                temp=lines[lines.index(s[i])]
                temp=temp.replace(before,after)
                replacelines.insert(lines.index(s[i])+1,temp)
                lines.insert(lines.index(s[i])+1,temp)
                lines.insert(lines.index(s[i])+1,temp)
                lines.pop(lines.index(s[i]))
            fp = open(path+"/"+file,'w')
            fp.writelines(replacelines)
            print(file + " is ok!")
            count += 1
        fp.close()
if 0 == count:
    print("没有文件匹配!")
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值