首页 > 解决方案 > 打开和关闭大量文件后,脚本python无缘无故停止运行

问题描述

我需要打开几个文件,逐行读取并归档。但是,我必须分析大量的行。在大约 240k 行之后,脚本似乎无缘无故地暂停了。

编码:

for folder in os.listdir("Data"):
    for subfolder in os.listdir("Data/"+folder):
        with open("Data/"+folder+'/'+subfolder, "r", encoding='utf8', errors='ignore') as f:
            for line in f:
                path = "Data/" + line[0] + "/"
                if not os.path.exists(path):
                    os.makedirs(path)
                with open(path + line[1] +".txt", "a", encoding="utf-8", errors="ignore") as f1:
                    f1.writelines(line)
                tested += 1
                print("folder: "+folder+", subfolder: "+subfolder+", tested: "+str(tested))

控制台只是停止打印而不停止脚本

标签: pythonpython-3.xlarge-files

解决方案


推荐阅读