Python Forum
Extract file only (without a directory it is in) from ZIPIP
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extract file only (without a directory it is in) from ZIPIP
#1
Greetings!
I'd like to extract files only, without the directories they are in, in the ZIP.
I wrote a small script but it extracts files with the directories.
Could you help me with this, please?
from zipfile import ZipFile
zp_dr   = 'C:\\02\\ZP_Files\\my.zip
dir_out = 'C:\\02\\ZP_Out

with zipfile.ZipFile(zp_dr) as zz:
       for file in zz.namelist():
            if 'Trace' in file :
                print (f"ZIP Item : {file}")
                zz.extract(file,dr_out)
Any help is appreciated.
Thank you!
Reply
#2
Use Zipfile.extractall(). I think it will do exactly what you want.

If you only want to extract files that match some kind of pattern, making extractall() a poor fit, you can specify an absolute path. This program extracts png files and puts them in my test folder.
from zipfile import ZipFile
from pathlib import Path

with ZipFile('games.zip', 'r') as zip:
    for file in zip.namelist():
        if Path(file).suffix == ".png":
            zip.extract(file, f'test/{Path(file).name}')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,135 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 1,135 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  Extract and rename a file from an Archive tester_V 4 3,991 Jul-08-2024, 07:54 AM
Last Post: tester_V
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 6,758 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 8,293 Jun-27-2023, 01:17 PM
Last Post: diver999
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 2,090 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  no such file or directory in SFTP saisankalpj 2 2,709 Nov-25-2022, 11:07 AM
Last Post: DeaD_EyE
Photo Making Zip file of a file and Directory Nasir 2 1,844 Oct-07-2022, 02:01 PM
Last Post: Nasir
  Failed to execute child process (No such file or directory) uriel 1 2,438 Sep-15-2022, 03:48 PM
Last Post: Gribouillis
  Need Help: FileNotFoundError:[Errno 2] No such file or directory python202209 5 6,281 Sep-12-2022, 04:50 AM
Last Post: python202209

Forum Jump:

User Panel Messages

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