Python Forum
Find specific file in an archive
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find specific file in an archive
#1
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.
Reply
#2
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))
tester_V likes this post
Reply
#3
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']
tester_V likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
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.
Reply
#5
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))
snippsat and tester_V like this post
Reply
#6
NO worries!
It works now.

Thank you! Big Grin
Reply
#7
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.
Reply
#8
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)
tester_V likes this post
Reply
#9
to BashBedlam:
Thank you!
your help is much appreciated!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting specific file from an archive tester_V 4 426 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Find a specific keyword after another keyword and change the output sgtmcc 5 746 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,495 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  Cannot find py credentials file standenman 5 1,554 Feb-25-2023, 08:30 PM
Last Post: Jeff900
  selenium can't find a file in my desk ? SouAmego22 0 701 Feb-14-2023, 03:21 PM
Last Post: SouAmego22
  Reading Specific Rows In a CSV File finndude 3 940 Dec-13-2022, 03:19 PM
Last Post: finndude
  Find (each) element from a list in a file tester_V 3 1,155 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Installing Pillow from remote archive in Linux Flatpak ChrisOfBristol 6 1,385 Sep-23-2022, 07:48 PM
Last Post: ChrisOfBristol
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,225 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  what will be the best way to find data in txt file? korenron 2 1,126 Jul-25-2022, 10:03 AM
Last Post: korenron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020