Python ZipFile Module - Runtime Issues

I am doing a little script to pin multiple folders in multiple ZIP files to a specific structure. I created the structure as a list. Here are a few entries:

['E:\Documents\UFSCar\Primeiro Ano\Primeiro Semestre\Cálculo 1', 'E:\Documents\UFSCar\Primeiro Ano\Segundo Semestre\Estatistica', 'E:\Documents\UFSCar\Primeiro Ano\Segundo Semestre\Estruturas Discretas', 'E:\Documents\UFSCar\Primeiro Ano\Segundo Semestre\Introdução à Engenharia'] 

Below are two methods that can be stapled to copy files together.

 def zipit (path, archname): # Create a ZipFile Object primed to write archive = ZipFile(archname, "w", ZIP_DEFLATED) # "a" to append, "r" to read # Recurse or not, depending on what path is if os.path.isdir(path): zippy(path, archive) else: archive.write(path) archive.close() return "Compression of \""+path+"\" was successful!" def zippy(path,archive): paths = os.listdir(path) for p in paths: p = os.path.join(path,p) if os.path.isdir(p): zippy(p,archive) else: archive.write(p) return 

The main part of the os script is this:

 for i in range(len(myList)): zipit(myList[i],os.path.split(myList[i])[1]) 

I used numeric indexes because it made the script work well for more files. Before that, we wrote only 2 zipfiles. Thus, about 8 make their way to the end. I do not know why.

The script simply iterates through the list and compresses each of them as a separate zip file. The problem occurs when the size of the list is larger. I get the following error message.

 Traceback (most recent call last): File "E:\Documents\UFSCar\zipit.py", line 76, in <module> zipit(listaDisciplinas[i],os.path.split(listaDisciplinas[i])[1]) File "E:\Documents\UFSCar\zipit.py", line 22, in zipit zippy(path, archive) File "E:\Documents\UFSCar\zipit.py", line 11, in zippy zippy(p,archive) File "E:\Documents\UFSCar\zipit.py", line 11, in zippy zippy(p,archive) File "E:\Documents\UFSCar\zipit.py", line 13, in zippy archive.write(p) File "C:\Python27\lib\zipfile.py", line 994, in write mtime = time.localtime(st.st_mtime) ValueError: (22, 'Invalid argument') 

Does anyone know what might cause this error? thanks!

EDIT:

I used the code below to check the files, the problem is with the files with problems with the timestamp of the last change. For some reason, it is unknown, some of them had the last modification in 2049.

In this case, the Python zipfile module could not compress the files when a ValueError was thrown.

My solution: edit the problem files to a random timestamp. Maybe someday I will check if there is a better solution.

Thanks for helping everyone.

+4
source share
3 answers

An error report related to this problem was submitted in 2007: http://bugs.python.org/issue1760357

The problem is caused by an error in the Windows local time function, and there is nothing that the time module could do except throw ValueError.

I have a problem:

 try: zip.write(absfilename, zipfilename) except ValueError: os.utime(absfilename, None) zip.write(absfilename, zipfilename) 

The os.utime line updates file access and changes the time to the current time.

+4
source

mtime is the timestamp when the last file was changed. Thus, it is probably incorrect for a single file. Find out which file calls it, then call os.stat(filename).st_mtime to check it.

0
source

See if this works better. At a minimum, you will find out which file fails and why.

 import os import os.path from time import localtime from zipfile import ZipFile, ZIP_DEFLATED def zipper(zipfilename, directory): archive = ZipFile(zipfilename, "w", ZIP_DEFLATED) for root, dirs, files in os.walk(directory): for f in files: path = os.path.join(root, f) try: archive.write(path) except ValueError, err: print "Error compressing %s" % path s = os.stat(path) print s.st_mtime print localtime(s.st_mtime) print str(err) archive.close() if __name__ == '__main__': zipper('foo.zip', '.') 
0
source

All Articles