简体   繁体   English

os.listdir 没有读取所有文件

[英]os.listdir not reading all files

I am trying to pull a particular line out of each file within a folder.我正在尝试从文件夹中的每个文件中提取特定行。 The code I wrote is opening each file and opening the new output file, although it is looping within each file and outputting data twice in some cases.我写的代码是打开每个文件并打开新的输出文件,尽管它在每个文件中循环并在某些情况下输出数据两次。 I have 15 files within about 800,000 lines between all files.我在所有文件之间的大约 800,000 行内有 15 个文件。

`import os
    for filename in os.listdir("path"):
       fin=open("path\%s" %filename)
       #print fin
       fout=open("newdata.txt","w")
       #print fout
       l=""
       for line in fin:
           p=line.strip().split("\t")
           if p[3]=="Cycle" and p[4]=="Protein":
               l+=line
              fout.write(l)
       #else:pass
  # fin.close()
  # fout.close()`

您在循环中以“w”模式打开文件,因此对于每个新文件,它将从头开始输出文件,您应该在循环外打开它或使用“w+”模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM