Python Forum
Find specific file in an archive - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Find specific file in an archive (/thread-32484.html)



Find specific file in an archive - tester_V - Feb-12-2021

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.


RE: Find specific file in an archive - BashBedlam - Feb-12-2021

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



RE: Find specific file in an archive - perfringo - Feb-12-2021

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']



RE: Find specific file in an archive - tester_V - Feb-12-2021

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.


RE: Find specific file in an archive - BashBedlam - Feb-12-2021

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



RE: Find specific file in an archive - tester_V - Feb-13-2021

NO worries!
It works now.

Thank you! Big Grin


RE: Find specific file in an archive - tester_V - Feb-13-2021

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.


RE: Find specific file in an archive - BashBedlam - Feb-13-2021

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)



RE: Find specific file in an archive - tester_V - Feb-13-2021

to BashBedlam:
Thank you!
your help is much appreciated!