Python Forum
Extracting specific file from an archive
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extracting specific file from an archive
#1
Greetings!
I need to extract a specific file from an archive. The archive is a zipped directory with subdirectories and a bunch of other files. I need to find a file named "Trace", rename it (I'm adding '1' to the front of the file), and copy it to a directory. I managed to do this, I find the file, and then I open each "Trace" file, read it, and then copy it to a directory.
I do not like reading and writing each file. I'd like just to rename and copy the "Trace" file to a new dir without reading it. Any help is appreciated.
Thank you!
from zipfile import ZipFile zp_dr = 'C:\\02\\ZP_Files\\Archive-1.zip' # <-- Zip File dr_out = 'C:\\02\\ARCHIVE\\' # <---------- OutPut Directory with zipfile.ZipFile(zp_dr,'r') as zz: for file in zz.namelist(): if 'Trace' in file : print (f" TraceLog File : {file}") fn = Path(file).name print(f" TraceLog File Name Only = {fn}") #zz.extract(fn, path=dr_out) # <------ Extracts both file and a directory dr_out_new = f"{dr_out}-1{fn}" # <-- Modifying File name print(f" NEW NAME FILE -- {dr_out_new}") with zz.open(file, mode="r") as trc, open(dr_out_new,'w', encoding='utf-8') as new_name: for el in trc : print(el) new_name.write(el.decode('utf-8')) exit 
Reply
#2
Extract to a temp directory and rename.
import os from zipfile import ZipFile from tempfile import TemporaryDirectory def extract(zip, searchname, destdir="."): version = 0 name, ext = searchname.split(".") with ZipFile(zip, "r") as zz, TemporaryDirectory() as tempdir: for file in zz.namelist(): if file.endswith(searchname): # Extract file to temporary folder zz.extract(file, tempdir) # Rename file to reside in destination directory. # If multiple matches, add version number to name. vstr = f"_{version}" if version > 0 else "" destination = rf"{destdir}\{name}{vstr}.{ext}" os.rename(rf"{tempdir}\{file}", destination) version += 1 extract("somezipfile.zip", "__init__.py", "test")
Reply
#3
(Jan-28-2024, 04:48 AM)tester_V Wrote: I'd like just to rename and copy the "Trace" file to a new dir without reading it. Any help is appreciated.
Thank you!
Have to read all files the zip directory structure as you do with zz.namelist(),or will not find a file.
There are some erros like wrong path when i test your code,here are changes then code work ok.
from zipfile import ZipFile from pathlib import Path zp_dr = 'test.zip' # <-- Zip File dr_out = 'C:/code/output_folder' with ZipFile(zp_dr,'r') as zz: for file in zz.namelist(): if 'find_me.txt' in file : print (f" TraceLog File : {file}") fn = Path(file).name print(f" TraceLog File Name Only = {fn}") #zz.extract(fn, path=dr_out) # <------ Extracts both file and a directory dr_out_new = f"{dr_out}/1{fn}" # Add / and not - print(dr_out_new) # Check that path is ok print(f" NEW NAME FILE -- {dr_out_new}") with zz.open(file, mode="r") as trc, open(dr_out_new,'w', encoding='utf-8') as new_name: for el in trc : print(el) new_name.write(el.decode('utf-8'))
Output:
TraceLog File : ff/2024/Jan_2024/2024-01-20/find_me.txt TraceLog File Name Only = find_me.txt C:/code/output_folder/1find_me.txt NEW NAME FILE -- C:/code/output_folder/1find_me.txt
tester_V likes this post
Reply
#4
The module codecs helps with these zip file outputs.

from zipfile import ZipFile import codecs path2zip = '/home/pedro/tmp/test_zipfile.zip' savepath = '/home/pedro/tmp/' def writeIt(anewname, data): with open(anewname, mode="w") as myfile: print(f'Writing {anewname} ... ') myfile.write(data) zin = ZipFile(path2zip, 'r') for item in zin.infolist(): if 'j' in item.filename: # get all files with j in their names newname = '1' + item.filename buffer = zin.read(item.filename) output = codecs.decode(buffer) writeIt(savepath + newname, output)
Output:
Writing /home/pedro/tmp/1Jingjing_css.txt ... Writing /home/pedro/tmp/1jiu_words.txt ...
tester_V likes this post
Reply
#5
Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extract and rename a file from an Archive tester_V 4 5,875 Jul-08-2024, 07:54 AM
Last Post: tester_V
  Extracting the correct data from a CSV file S2G 6 2,949 Jun-03-2024, 04:50 PM
Last Post: snippsat
  Reading Specific Rows In a CSV File finndude 3 2,590 Dec-13-2022, 03:19 PM
Last Post: finndude
  Installing Pillow from remote archive in Linux Flatpak ChrisOfBristol 6 3,607 Sep-23-2022, 07:48 PM
Last Post: ChrisOfBristol
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 2,865 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Extracting Specific Lines from text file based on content. jokerfmj 8 7,371 Mar-28-2022, 03:38 PM
Last Post: snippsat
  [Solved] Trying to read specific lines from a file Laplace12 7 6,190 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  Extract specific sentences from text file Bubly 3 6,108 May-31-2021, 06:55 PM
Last Post: Larz60+
  Find specific file in an archive tester_V 8 5,863 Feb-13-2021, 06:13 PM
Last Post: tester_V
  Writing to file in a specific folder evapa8f 5 5,688 Nov-13-2020, 10:10 PM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.