Python zipfile崩溃为某些文件

时间:2015-04-29 06:54:49

标签: python zipfile

我有一个使用zipfile模块压缩文件的简单代码。我能够压缩一些文件,但是其他文件出现FileNotFound错误。我检查过这是文件大小错误但不是。

我可以打包名称为example file.py的文件,但是当我在一个目录中有一个文件时,例如分析文件en-US_es-ES.xlsx'如果失败了

当我将os.path.basename更改为os.path.join我不想压缩整个文件夹结构时,它会起作用,我想在我的拉链中使用扁平结构。

这是我的代码:

import os
import zipfile

path = input()

x=zipfile.ZipFile('new.zip', 'w')
for root, dir, files in os.walk(path):
    for eachFile in files:        
        x.write(os.path.basename(eachFile))
x.close()

错误如下所示:

Traceback (most recent call last):  
File "C:/Users/mypc/Desktop/Zip test.py", line 15, in <module>    
x.write(os.path.basename(eachFile))  
File "C:\Python34\lib\zipfile.py", line 1326, in write    
st = os.stat(filename)

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Analyze Files en-US_ar-SA.xlsx'*

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

只需更改工作目录即可添加没有原始目录结构的文件。

course_id = int(request.POST['course'])
if not Status.objects.get(course__pk=course_instance, user=request.user.profile, role="student"):
            rel = Status(course=course_id, user=request.user.profile,
                         role="student")
            rel.save()
相关问题