python 转换代码格式

本文介绍了一个Python脚本,用于遍历指定目录下的所有文件,并进行特定文本的读取、修改和删除操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import os
dirname="C:\\Users\\haier\\Desktop\\new"

def walk(path):
    for item in os.listdir(path):
        subpath = os.path.join(path, item)
        #subpath=subpath.replace('\\','\\\\')
        print subpath
        for i in open(subpath):
            pass#print i
        ffrom=open(subpath,'r')
        fto=open(subpath+"1",'w')
        while True:
            l=ffrom.readline()
            if not l:
                break
            if 'endif' in l:
                print l
                l=l.replace('endif','end')
                print l
            elif 'endwhile' in l:
                print l
                l=l.replace('endwhile','end')
                print l                
            # if '\"' in l:
                # print l
                # l=l.replace('\"','\'')
                # print l
              #  print l
               # tmp=l.split("\"")
                #l=tmp[0]+"\'"+tmp[1]
            fto.write(l)                      
        ffrom.close()
        fto.close()

def dele(path):
   for item in os.listdir(path):
        subpath = os.path.join(path, item)
       # subpath=subpath.replace('\\','\\\\')

            
        if os.path.exists(subpath+"1"):
            ffrom=open(subpath+"1",'r')
            fto=open(subpath,'w')
            while True:
                l=ffrom.readline()
                if not l:
                    break
                fto.write(l)                      
            ffrom.close()
            fto.close()
            os.remove(subpath+"1")

        
walk(dirname)
dele(dirname)

 

转载于:https://www.cnblogs.com/huashiyiqike/p/3245530.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值