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.