1

I'm trying to extract data from a zip file in Python, but it's kind of slow. Could anyone advise me and see if I'm doing something that obviously makes it slower?

def go_through_zip(zipname):
    out = {}
    
    with ZipFile(zipname) as z:
        for filename in z.namelist():
            with z.open(filename) as f:
                try:
                    outdict = make_dict(f)
                    out.update(outdict)
                except:
                    print("File is not in the correct format")
    
    return out 

make_dict(f) just takes the file path and makes a dictionary, and this function is probably also slow, but that's not what I want to speed up right now.

Ada
  • 71
  • 3
  • do you extract it only to dictionary or on disk? You could try to use module `time` or `profiler` in some `IDE` to see which part works slow. – furas Jun 24 '21 at 07:15
  • https://stackoverflow.com/questions/4997910/python-unzip-tremendously-slow, https://stackoverflow.com/questions/61930445/fast-zip-decryption-in-python – Albert Aug 24 '23 at 20:41

1 Answers1

0

Try using the following code for file extraction. it works fast as long as the size of the file being extracted is reasonable.

# importing required modules
from zipfile import ZipFile
  
# specifying the zip file name
file_name = "my_python_files.zip"
  
# opening the zip file in READ mode
with ZipFile(file_name, 'r') as zip:
    # printing all the contents of the zip file
    zip.printdir()
  
    # extracting all the files
    print('Extracting all the files now...')
    zip.extractall()
    print('Done!')
    ```
Harris Minhas
  • 702
  • 3
  • 17
  • 1
    Thanks, but I need the file name of each file to pass on to the function make_dict? How can I do that? (It doesn't seem like this is done in your code) – Ada Jun 24 '21 at 07:20
  • It doesnt matter, the slowness is probably because when opening the zip file, you are not setting the permission as ```ZipFile(file_name, 'r')```. My code only extracts the data from the code as you demanded in the first line of your question. – Harris Minhas Jun 24 '21 at 07:23
  • 1
    Hm, what do you mean by setting permission? – Ada Jun 24 '21 at 07:31