Python中的ZipFile模块

编程入门 行业动态 更新时间:2024-10-25 16:29:30
Python中的ZipFile模块 - 运行时问题(ZipFile module in Python - Runtime problems)

我正在做一个小脚本来按照特定结构压缩多个zip文件中的多个文件夹。 我已经将结构构建为列表。 以下是一些条目:

['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']

这里有两种方法可以将文件压缩在一起。

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

脚本的主要部分是这样的:

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

我使用了数字索引,因为这使得脚本可以很好地运行大量文件。 在此之前,我们只写了2个zipfiles。 这样,大约8个到达终点。 不知道为什么。

该脚本只是遍历列表并将每个压缩为压缩文件。 当列表的大小更大时会发生问题。 我收到以下错误消息。

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')

有谁知道什么可能导致这个错误? 谢谢!

编辑:

我已经使用下面提供的代码来测试文件,问题是他们的“最后修改”时间戳有问题的文件。 由于某些原因未知,其中一些在2049年进行了最后修改。

在这种情况下,Python zipfile模块在压缩文件时失败,因为抛出了ValueError。

我的解决方案:编辑有问题的文件以获取其时间戳。 也许有一天我会验证是否有更好的解决方案。

感谢大家的帮助。

I'm doing a little script to zip multiple folders in multiple zip files following a certain structure. I've built the structure as a list. Here are some 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']

Down here are the 2 methods resposible for zipping the 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 os the script is like this:

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

I've used numerical indexes because this made the script run well for a larger number of files. Before that, only 2 zipfiles we're written. This way, about 8 make its way to the end. No clue why.

The script simply iterates over the list and compresses each one as a separated zipfile. The problem happens when the size of the list is bigger. 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')

Do anyone knows what may cause this error? thanks!

EDIT:

I've used the code provided below to teste the files, the problem were the files with problems with their "last modified" timestamp. For some reason unknown, some of them had the the last modification in the 2049 year.

In that case, the Python zipfile module failed in compressing the files as a ValueError was thrown.

My solution: edit the problematic files to chance their timestamp. Maybe someday I verify is there is a better solution.

Thanks for the help of everyone.

最满意答案

2007年提交了与此问题相关的错误报告: http : //bugs.python.org/issue1760357

问题是由Windows localtime函数中的错误引起的,并且除了抛出ValueError之外,没有时间模块可以做的事情。

我解决了这个问题:

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

os.utime行将文件的访问权限和修改时间更新为当前时间。

A bug report related to this issue was submitted in 2007: http://bugs.python.org/issue1760357

The problem is caused by a bug in the Windows localtime function and there's nothing the time module can do other than throw a ValueError.

I got around the problem like this:

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

The os.utime line update the file's access and modified times to the current time.

更多推荐

本文发布于:2023-07-24 19:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250195.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   Python   ZipFile

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!