简体   繁体   English

Python 遍历文件,搜索特定字符串,如果找到,则复制这些行的 rest 并合并到一个组合文件中

[英]Python Iterate through files, Search for certain string, if found copy rest of the lines and consolidate to a combined file

I have a folder contains 500 text files.我有一个包含 500 个文本文件的文件夹。 Python Iterate through files Search for certain string if found copy and consolidate to a combined file as "Output.txt". Python 遍历文件 搜索特定字符串,如果找到副本并合并为“Output.txt”的组合文件。 The string we are looking for in each of the file in the directory我们在目录中的每个文件中查找的字符串

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()

I am seeing the following error我看到以下错误

    with Output as f2:
AttributeError: __enter__

you cannot do with Output as f2 , because Output is a list and it doesn't support that, and gives you AttributeError: __enter__ , another problem is the line where you did f2.write() again you cannot write to a list, use append() instead.您不能将with Output as f2 ,因为Output是一个列表,它不支持它,并给您AttributeError: __enter__ ,另一个问题是您再次f2.write()的行,您无法写入列表,请使用append()代替。

Here is the full working code, i tested it:这是完整的工作代码,我对其进行了测试:

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()

Why not simply using a cmd line, go to the directory and run:为什么不简单地使用 cmd 行 go 到目录并运行:

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

In case you do not have egrep:如果您没有 egrep:

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

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

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