Python zipfile freezes when writing

I am trying to use the zipfile module in Python to create simple zip files:

import zipfile

files = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
zip_file_name = 'zipfile_test.zip'

zfh = zipfile.ZipFile(zip_file_name, 'w')
for file in files:
  print 'Archiving file %s' % file
  zfh.write(zip_file_name)
zfh.close()

The ah files are in my working directory and are empty, use touch a b c d e f g hfor verification.

After adding the first 7 elements to the zip file, it hangs on the last, but continues to write to the zip file until the space is exhausted. This happens on the two systems I tested on, one with Python 2.4.3, the other with Python 2.6.2. If the number of files is less than 6 or 7, a zip file is created without any problems. Otherwise, it is interrupted after 7-15 files and begins to write garbage to the end of the file. I tried to change:

  zfh.write(zip_file_name)

at

  zfh.write(zip_file_name, zip_file_name, zipfile.ZIP_DEFLATED)

which sometimes allows me to write a couple more files, but inevitably fails.

What am I doing wrong?

+3
1

zip zip :

zfh.write(zip_file_name)

:

zfh.write(file)
+9

Source: https://habr.com/ru/post/1729413/


All Articles