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
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,393 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 1,133 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  no such file or directory in SFTP saisankalpj 2 1,564 Nov-25-2022, 11:07 AM
Last Post: DeaD_EyE
Photo Making Zip file of a file and Directory Nasir 2 1,037 Oct-07-2022, 02:01 PM
Last Post: Nasir
  Failed to execute child process (No such file or directory) uriel 1 1,665 Sep-15-2022, 03:48 PM
Last Post: Gribouillis
  Need Help: FileNotFoundError:[Errno 2] No such file or directory python202209 5 2,672 Sep-12-2022, 04:50 AM
Last Post: python202209
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,284 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,388 May-17-2022, 07:49 AM
Last Post: Pedroski55
  How to read python shortcut target profile directory of Chrome Ink file sunny9495 1 1,670 Apr-12-2022, 06:12 PM
Last Post: sunny9495
  Extract parts of a log-file and put it in a dataframe hasiro 4 6,351 Apr-08-2022, 01:18 PM
Last Post: hasiro

Forum Jump:

User Panel Messages

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