Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在压缩和解压缩文件夹中搜索特定行_Python_Regex_Python 3.x_Search - Fatal编程技术网

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

Python 如何在压缩和解压缩文件夹中搜索特定行,python,regex,python-3.x,search,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

我正在尝试实现一个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-and-except块是我试图打开压缩文件夹并读取它。我仍然不太清楚如何打开压缩文件夹或它是如何工作的。请让我知道如何修改我的代码,使其工作,非常感谢

一种可能性是首先确定使用的对象类型
文件夹\u 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:
        ...

你试过使用吗?类似于
myzip=zipfile.zipfile(文件夹名称)print myzip.namelist()
的内容会将文件名保存在zip文件存档中。您好,感谢您的回复。我尝试了你的建议,但最终得到了错误“TypeError:需要一个类似字节的对象,而不是'str'。你知道为什么吗?谢谢对不起,也许我完全误解了。您使用的是压缩的
“SampleZippedFolder.zip”
文件还是
“SampleZippedFolder”
文件夹
ZipFile
需要的是
zip
文件归档,而不是文件文件夹。也许您传递的是文件夹而不是邮政编码?与此演示相比:感谢您的澄清,我确实正在传递一个文件夹。好的,但为什么您仍在传递该文件夹?我以为你想用压缩文件?“不知道如何对输入功能的压缩文件夹执行相同操作。”