如何使用python在大文件中获取具有许多类似行的特定行?

如何使用python在大文件中获取具有许多类似行的特定行?,python,text,readline,Python,Text,Readline,在不同的目录中具有相同名称的不同文件。在这些文件中,有几行几乎相等,我只想取出其中的最后一行(后面还有更多行),并将其写入另一个文件中 到目前为止,我所做的: #!/usr/bin/env python import os def cd_grep(): for file in os.listdir("."): if os.path.isfile(file): for line in open("graph.txt"): if

在不同的目录中具有相同名称的不同文件。在这些文件中,有几行几乎相等,我只想取出其中的最后一行(后面还有更多行),并将其写入另一个文件中

到目前为止,我所做的:

#!/usr/bin/env python

import os

def cd_grep():
   for file in os.listdir("."):
     if os.path.isfile(file):
       for line in open("graph.txt"):
                  if " 4.49" in line:                               
                       line_list=[line] 
   g = open('comparation','a') 
   g.write ("%s" % (line[0:4]))
   g.close()
os.chdir('4.294')
cd_grep()
os.chdir(os.pardir)
os.chdir('4.394')
cd_grep()
os.chdir(os.pardir)
os.chdir('4.494')
cd_grep()
os.chdir(os.pardir)
os.chdir('4.594')
cd_grep()
os.chdir(os.pardir)
os.chdir('4.694')
cd_grep()
我创建了一个列表,因为我将只获取整条线的特定信息

最后,我发现这个过程只适用于小文件,并且只适用于文件的最后一行包含我正在搜索的术语的情况。 对于大文件,我收到了这样一条信息(在文件中,我希望得到这一行):
自愿性上下文切换:3403


任何想法或建议都将不胜感激。

我猜您没有关闭您的文件

def cd_grep():
    for file in os.listdir("."):
        if os.path.isfile(file):
           graph_file = open('graph.txt'):
           for line in graph_file:
               if " 4.49" in line:                               
                   line_list=[line] 
           graph_file.close()
    g = open('comparation','a') 
    g.write ("%s" % (line[0:4]))
    g.close()
def cd_grep():
    for file in os.listdir("."):
        if os.path.isfile(file):
           with open('graph.txt') as graph_file:
               for line in graph_file:
                   if " 4.49" in line:                               
                       line_list=[line] 
    with open('comparation','a') as g:
        g.write ("%s" % (line[0:4]))
或者最好使用
一起打开(并始终关闭)文件

def cd_grep():
    for file in os.listdir("."):
        if os.path.isfile(file):
           graph_file = open('graph.txt'):
           for line in graph_file:
               if " 4.49" in line:                               
                   line_list=[line] 
           graph_file.close()
    g = open('comparation','a') 
    g.write ("%s" % (line[0:4]))
    g.close()
def cd_grep():
    for file in os.listdir("."):
        if os.path.isfile(file):
           with open('graph.txt') as graph_file:
               for line in graph_file:
                   if " 4.49" in line:                               
                       line_list=[line] 
    with open('comparation','a') as g:
        g.write ("%s" % (line[0:4]))

不确定您收到的错误(上次编辑后)

我已经试着重写了一点代码,希望它能给你一个类似于你所需要的结果(警告:未测试)


首先,格式化你的代码,使之可读并正确缩进。好的,谢谢。我第一次在这里提出问题。我希望现在更好。现在显示您得到的确切错误。这是个例外吗?你的节目结束了吗?你在哪里看到那条消息?我在文件中找到了错误,我希望在那里得到那条线。我不知道这是否是一个例外(因为我不知道它的确切含义)。程序完成。您确定“打开中的行”(“graph.txt”)的行
缩进正确吗?如果您只对“graph.txt”感兴趣,为什么要检查目录中的所有文件?它工作得很好。唯一的问题是,出于某种原因,它多次给我相同的输出。文件:/path1/,行:xxxxxx文件:/path1/,行:xxxxxx文件:/path1/,行:xxxxxx文件:/path1/,行:xxxxxx文件:/path2/,行:yyyyyyyyyyy文件:/path2/,行:yyyyyyyyyyy文件:/path2/,行:yyyyyyyyyyyyyyyy我应该在循环外写入文件(比较)吗?对不起,我无法测试它。我认为您应该能够在交互式python解释器中使用它;没有测试文件,我无法很好地回答你。我还忘了在附加到文件(现在添加)时添加“\n”,这似乎在write_文件中形成了一条长线。希望现在效果更好;玩得开心,我很抱歉。我没有删除代码的前两行。这样做之后,效果非常好。非常感谢你的关注和提示。但它仍然占用文件的最后一行,即使它不是正确的一行。