首页 > 解决方案 > 删除字符串中的第一个元素时创建的空文件

问题描述

我的代码是 -

import os


currentdir = "........."
resultdir="............."


for root, dirs, files in os.walk(currentdir):
        for name in files:
            outfile1 = open(resultdir + "/" + name, "w+")
            #outfile1 = open(resultdir + "/" + name, "w+")
            print(name)
            outfile2 = open(root+"/"+name,'r')
            line = outfile2.readline()
            while line:
                #print(line)

                if line[0]!="\"":

                    print(line)
                    outfile1.write(line)
                    outfile1.write("\n")
                line = outfile2.readline()  
            outfile2.close()
            outfile1.close()

我正在尝试从目录中读取每个文件,并且在每个文件中我试图省略开头有“”的行并将这些更正的文件保存在新目录中。但是我在新目录中得到空文件。

标签: pythonfile

解决方案


推荐阅读