如何在压缩和解压缩的文件夹中搜索特定行

时间:2018-06-26 16:55:32

标签: python regex python-3.x search

我正在尝试实现一个Python脚本,该脚本从用户处获取一个文件夹(可以压缩或解压缩),并搜索该文件夹中的所有文件以输出与我的正则表达式匹配的特定行。下面的代码适用于常规的未压缩文件夹,但是我无法弄清楚如何对输入功能的已压缩文件夹执行相同的操作。下面是我的代码,在此先感谢!

def myFunction(folder_name):


path = folder_name


for (path, subdirs, files) in os.walk(path): 
    files = [f for f in os.listdir(path) if f.endswith('.txt') or f.endswith('.log') or f.endswith('-release') or f.endswith('.out') or f.endswith('messages') or f.endswith('.zip')] # Specify here the format of files you hope to search from (ex: ".txt" or ".log")
    files.sort() # file is sorted list

    files = [os.path.join(path, name) for name in files] # Joins the path and the name, so the files can be opened and scanned by the open() function

    # The following for loop searches all files with the selected format
    for filename in files:


            #print('start parsing... ' + str(datetime.datetime.now()))
            matched_line = []
            try:         
                 with open(filename, 'r', encoding = 'utf-8') as f:
                        f = f.readlines()
            except:      
                 with open(filename, 'r') as f:
                        f = f.readlines()                     

            # print('Finished parsing... ' + str(datetime.datetime.now()))

             for line in f:
                #0strip out \x00 from read content, in case it's encoded differently
                line = line.replace('\x00', '')

                RE2 = r'^Version: \d.+\d.+\d.\w\d.+'
                RE3 = r'^.+version.(\d+.\d+.\d+.\d+)' 
                pattern2 = re.compile('('+RE2+'|'+RE3+')', re.IGNORECASE)
                for match2 in pattern2.finditer(line):
                    matched_line.append(line)
                    print(line)


#Calling the function to use it

myFunction(r"SampleZippedFolder.zip")

我的代码的try andexcept块是我尝试打开压缩文件夹并读取它。我仍然不太清楚如何打开压缩文件夹或它的工作方式。请让我知道如何修改代码以使其正常工作,非常感谢!

1 个答案:

答案 0 :(得分:0)

一种可能性是,首先确定正在使用zipfilefolder_name的对象类型os.isdir(),然后无论哪个成功,获取文件列表并继续。也许是这样的:

import zipfile, os, re

def myFunction(folder_name):

    files = None # nothing yet

    path = folder_name

    if zipfile.is_zipfile(path):
        print('ZipFile: {}'.format(path))
        f = zipfile.ZipFile(path)
        files = f.namelist()
        # for name in f.namelist(): # debugging
            # print('file: {}'.format(name))

    elif os.path.isdir(path):
        print('Folder: {}'.format(path))
        files = os.listdir(path)
        # for name in os.listdir(path): # debugging
        #     print('file: {}'.format(name))

    # should now have a list of files
    # proceed processing the files 
    for filename in files:
        ...