Jul-07-2024, 04:14 AM
Greetings and happy Holidays!
I’m trying to extract a specific file from archives without the whole directory structure.
Each archive holds the same file name, I have to rename it or it’ll be overwritten.
I thought adding a time stamp to the file name or just replacing the name with the time stamp will be a good idea.
I can pull out the file I need, it works fine, but I’m having a hard time renaming it during the extraction.
Should I rename the file after it is extracted?
Here is the code:
Thank you.
I’m trying to extract a specific file from archives without the whole directory structure.
Each archive holds the same file name, I have to rename it or it’ll be overwritten.
I thought adding a time stamp to the file name or just replacing the name with the time stamp will be a good idea.
I can pull out the file I need, it works fine, but I’m having a hard time renaming it during the extraction.
Should I rename the file after it is extracted?
Here is the code:
with zipfile.ZipFile(ef) as zip:
for items in zip.infolist():
ff = items.filename
fn = Path(ff).name
#print(f" File Names in the Archive :: {fn}")
if fn.startswith("Loop") and fn.endswith(".txt") :
timestm = datetime.now().strftime('%H%M%S%m%d%Y') # ----- Timestamp to add to the file name
print(f" {timestm}")
mm =items.filename = os.path.basename(items.filename) # - Exctacts File only
print(' FILE TO Copy .....', mm)
time.sleep(1)
try :
zip.extract(items, trl_dr_out)
except BadZipfile as bzip:
print(f" BAD ZIP {bzip}")
pass Any help is appreciated.Thank you.
