os.listdir 没有读取所有文件

os.listdir not reading 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+' 模式