Python Forum

Full Version: Find specific file in an archive
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I hope you have better weather than we have tonight in Portland Oregon.
Anyway,
I need to extract a specific file that starts with the word "Debug*.log" from an archive.
An archive may or may not have the file, also the archive is actually a zipped directory with SubDirs and files.
The file I'm looking for could be any of the SubDirs.
I tried to use "zip.namelist" but it only prints files but not recursively,

zip_filepath = 'C:\\02\\TEST-3.zip'
target_dir   = 'C:\\03\\'

zip = zipfile.ZipFile(zip_filepath)
print (zip.namelist())
I understand I need to test the Archive first but not sure how to do that.

Thank you.
Try this. I think it will do the trick for you.

from zipfile import ZipFile

with ZipFile (zip_filepath, 'r') as zip_file :
	file_name_list = zip_file.namelist ()
	for file_name in file_name_list :
		if 'Debug' in file_name :
			output_file_name = target_dir + file_name.split ('\')[-1]
			with open (output_file_name, 'wb') as out_file :
				out_file.write (zip_file.read (file_name))
Python has built-in module fnmatch (Unix filename pattern matching) which can be used to directly implement 'Debug*.log' pattern. This module also have filter, so one can write:

>>> import fnmatch
>>> filenames = ['Debug1.log', 'Debug1.txt', 'Debug.log']
>>> fnmatch.filter(filenames, 'Debug*.log')
['Debug1.log', 'Debug.log']
TO:
BashBedlam
perfringo

Thank you both!

I tried just the BeshBedam code because I'm not sure how to 'perfringo's" snippet.

I'm having an error:
output_file_name = target_dir + file_name.split ('\')[-1]
^
SyntaxError: EOL while scanning string literal

Thank you.
Sorry, I forgot to escape the backslash character. I tested it on a linux system and modified it just before posting to what I thought would be compatible with windows. Try this and if it doesn't work, maybe someone on a windows system can make the output_file_name correct.

from zipfile import ZipFile
 
with ZipFile (zip_filepath, 'r') as zip_file :
    file_name_list = zip_file.namelist ()
    for file_name in file_name_list :
        if 'Debug' in file_name :
            output_file_name = target_dir + file_name.split ('\\')[-1]
            with open (output_file_name, 'wb') as out_file :
                out_file.write (zip_file.read (file_name))
NO worries!
It works now.

Thank you! Big Grin
One more question.
I have a lot of Zip files, I wanted to find ZIp files that have the "Debugxxx.log"
Is it possible to identify which ZIP file has "Debugxxx.log" in it?
I'd like to copy that file too.

Thank you.
Please let me know if this works on your system.

import os
location = 'C:\\02\\'
files_in_dir = []

# r=>root, d=>directories, f=>files
for r, d, f in os.walk(location):
	for item in f:
		if '.zip' in item:
			files_in_dir.append(os.path.join(r, item))

for item in files_in_dir:
	print("file in dir: ", item)
to BashBedlam:
Thank you!
your help is much appreciated!