Я пытаюсь реализовать скрипт 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")
Попробовать и исключить блок моего кода было моей попыткой открыть zip-папку и прочитать ее. Мне все еще не очень понятно, как открыть zip-папку или как она работает. Пожалуйста, дайте мне знать, как я могу изменить свой код, чтобы он работал, очень признателен!