Python迭代文件,搜索特定字符串,若找到,复制其余行并合并为一个组合文件

Python迭代文件,搜索特定字符串,若找到,复制其余行并合并为一个组合文件,python,Python,我有一个包含500个文本文件的文件夹。 Python在文件中迭代搜索特定字符串(如果找到),将其复制并合并为一个组合文件“Output.txt”。 我们在目录中的每个文件中查找的字符串 import os searchquery = 'No' #string we are looking for in each of the file in the directory def my_function(fname): Output=[] with fname as f1:

我有一个包含500个文本文件的文件夹。 Python在文件中迭代搜索特定字符串(如果找到),将其复制并合并为一个组合文件“Output.txt”。 我们在目录中的每个文件中查找的字符串

import os

searchquery = 'No' #string we are looking for in each of the file in the directory
def my_function(fname):
    Output=[]
    with fname as f1:
      with Output as f2:

        Lines = f1.readlines()

        try:
          i = Lines.index(searchquery)
          for iline in range(i+1, i+18): # we need to copy rest of the 18 or less line after 'No' is found
            f2.write(Lines[iline])
        except:
          print(" ")
    return Output

for filename in os.listdir('C:\\Users\\XXX\\Desktop\\Tox\\tcm2'):
    M1=open(filename)
    M2=my_function(M1)
    opened_file = open(Output.txt, 'a')
    opened_file.write("%r\n" % M1)
    opened_file.close()
我看到以下错误

    with Output as f2:
AttributeError: __enter__

输出为f2时不能执行
,因为
Output
是一个列表,它不支持该列表,并为您提供
AttributeError:\uuuuu enter\uuuuu
,另一个问题是执行
f2.write()
的行。如果无法写入列表,请改用
append()

以下是完整的工作代码,我对其进行了测试:

import os
searchquery = 'No'
path = 'C:\\Users\\XXX\\Desktop\\Tox\\tcm2\\'

def my_function(fname):
    Output=[]           
    Lines = fname.readlines()   
    found = False 
    for line in Lines :
        if (found == True):
            Output.append(line)
        if line.startswith(searchquery):
            found = True
    return Output

opened_file = open('Output.txt', 'a')
for filename in os.listdir(path):
    M1=open(path+filename)
    result=my_function(M1)        
    for e in result:
        opened_file.write(e)        
    M1.close()
opened_file.close()

输出为f2时不能执行
,因为
Output
是一个列表,它不支持该列表,并为您提供
AttributeError:\uuuuu enter\uuuuu
,另一个问题是执行
f2.write()
的行。如果无法写入列表,请改用
append()

以下是完整的工作代码,我对其进行了测试:

import os
searchquery = 'No'
path = 'C:\\Users\\XXX\\Desktop\\Tox\\tcm2\\'

def my_function(fname):
    Output=[]           
    Lines = fname.readlines()   
    found = False 
    for line in Lines :
        if (found == True):
            Output.append(line)
        if line.startswith(searchquery):
            found = True
    return Output

opened_file = open('Output.txt', 'a')
for filename in os.listdir(path):
    M1=open(path+filename)
    result=my_function(M1)        
    for e in result:
        opened_file.write(e)        
    M1.close()
opened_file.close()

为什么不简单地使用cmd行,转到目录并运行:

grep no -A18 * | egrep -v "no|--" > output.txt
如果您没有白鹭:

grep no -A18 * | grep -v no | grep -v "--" > output.txt

为什么不简单地使用cmd行,转到目录并运行:

grep no -A18 * | egrep -v "no|--" > output.txt
如果您没有白鹭:

grep no -A18 * | grep -v no | grep -v "--" > output.txt


fname为f1的
和输出为f2的
有什么意义?你希望他们做什么?非常感谢。我也认为没有必要。由于我有一个外部for循环来迭代文件。先生,但是我如何搜索特定字符串请查看文本文件..filebin.net/03o53ixhjitshsgb/tcm2.zip?t=savks9cu fname为f1
和输出为f2
有什么意义?你希望他们做什么?非常感谢。我也认为没有必要。由于我有一个外部for循环来迭代文件。先生,但是我如何搜索特定字符串请查看文本文件..filebin.net/03o53ixhjitshsgb/tcm2.zip?t=savks9cut非常感谢您,但仍然面临“opened_file=open(Output.txt,'a')opened_file.write(“%r\n”%M1)opened_file.close()部分的错误错误已打开\u file=open(Output.txt,'a')name错误:名称“Output”未定义请查看我编辑的答案,希望能解决您的所有问题,您有很多问题,再次感谢,但仍然有相同的问题在线。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。“opened_file=open(Output.txt,'a')”……错误。。。。。。。。。。。。。。。。。。NameError:name'Output'未定义
opened\u file=open('Output.txt','a')
我会一直在这里,直到你的代码运行为止,欢迎:)代码运行,但输出文件为空。我正在搜索的几个文件。。。。。。从我上传到的tcm2文件夹中。非常感谢您,但在“opened_file=open(Output.txt,'a')opened_file.write(“%r\n”%M1)opened_file.close()”这一节中仍然面临错误。错误opened_file=open(Output.txt,'a')name错误:未定义名称“Output”.请查看我编辑过的答案,希望能解决你所有的问题,你有很多问题,再次感谢你,但仍然有相同的问题在线上。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。“opened_file=open(Output.txt,'a')”……错误。。。。。。。。。。。。。。。。。。NameError:name'Output'未定义
opened\u file=open('Output.txt','a')
我会一直在这里,直到你的代码运行为止,欢迎:)代码运行,但输出文件为空。我正在搜索的几个文件。。。。。。从我上传的tcm2文件夹。到…对不起,这是什么“|”我没有在键盘上设置它。那些是窗户用的吗?我不在Linux中。。。它显示错误………..“grep”未被识别为内部或外部命令、可操作程序或批处理文件。powershell肯定有grep,可能没有,或者……grep:术语“grep”未被识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。在第1行:char:1hmmm,我的工作windows powershell中有grep,可能是我的公司为我们安装的。对不起,这是什么“|”我没有在键盘上设置它。那些是窗户用的吗?我不在Linux中。。。它显示错误………..“grep”未被识别为内部或外部命令、可操作程序或批处理文件。powershell肯定有grep,可能没有,或者……grep:术语“grep”未被识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。在第1行char:1hmmm,我的工作windows powershell中有grep,也许我的公司为我们安装了它。