python批量处理文件夹下所有文件的字符串

1.批量删除文件夹下每个文件中相邻的字符:

#!/usr/bin/python

import os

path = "./DATA"
files = os.listdir(path)

for file in files:
    print(file)
    f = open(path + "/" + file, "r")
    f_out = open(path + "/" + file + ".bak", "w")
    f.seek(1, 1)
    while True:
        str = f.read(1)
        if not str:
            break
        print(str)
        f_out.write(str)
        f.seek(1, 1)
    f.close()
    f_out.close()


path1 = "./DATA"
files1 = os.listdir(path1)

for file1 in files1:
    print(file1)
    print(file1.find(".bak"))
    if 0 < file1.find(".bak"):
        os.rename(path1 + "/" + file1, path1 + "/" + file1 + ".koson")

注释:1.首先读取文件夹下某个文件的字符串,跳过相邻的字符保存到另一个文件中,然后遍历文件夹下的所有文件进行处理;2.再遍历文件夹下所有新建的文件进行重命名;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值