Jan-28-2024, 04:48 AM
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!
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