使用python将文件夹添加到zip文件 [英] Adding folders to a zip file using python

查看:40
本文介绍了使用python将文件夹添加到zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 zip 文件.在 zip 文件中添加一个文件夹,然后将一堆文件添加到该文件夹​​中.

I want to create a zip file. Add a folder to the zip file and then add a bunch of files to that folder.

所以我想最终得到一个包含文件的单个文件夹的 zip 文件.

So I want to end up with a zip file with a single folder with files in.

我不知道将文件夹放在 zip 文件中是否是坏习惯,但谷歌没有给我任何关于这个主题的信息.

I dont know if its bad practice to have folders in zip files or something but google gives me nothing on the subject.

我是从这个开始的:

def addFolderToZip(myZipFile,folder):
    folder = folder.encode('ascii') #convert path to ascii for ZipFile Method
    for file in glob.glob(folder+"/*"):
            if os.path.isfile(file):
                print file
                myZipFile.write(file, os.path.basename(file), zipfile.ZIP_DEFLATED)
            elif os.path.isdir(file):
                addFolderToZip(myZipFile,file)

def createZipFile(filename,files,folders):
    curTime=strftime("__%Y_%m_%d", time.localtime())
    filename=filename+curTime;
    print filename
    zipFilename=utils.getFileName("files", filename+".zip")
    myZipFile = zipfile.ZipFile( zipFilename, "w" ) # Open the zip file for writing 
    for file in files:
        file = file.encode('ascii') #convert path to ascii for ZipFile Method
        if os.path.isfile(file):
            (filepath, filename) = os.path.split(file)
            myZipFile.write( file, filename, zipfile.ZIP_DEFLATED )

    for folder in  folders:   
        addFolderToZip(myZipFile,folder)  
    myZipFile.close()
    return (1,zipFilename)


(success,filename)=createZipFile(planName,files,folders);

取自:http://mail.python.org/pipermail/python-list/2006-August/396166.html

删除所有文件夹并将目标文件夹(及其子文件夹)中的所有文件放入单个 zip 文件中.我无法添加整个文件夹.

Which gets rid of all folders and puts all files in the target folder (and its subfolders) into a single zip file. I couldnt get it to add an entire folder.

如果我在 myZipFile.write 中输入文件夹的路径,我会得到

If I feed the path to a folder in myZipFile.write, I get

IOError: [Errno 13] 权限被拒绝: '..packedin'

IOError: [Errno 13] Permission denied: '..packedin'

非常欢迎任何帮助.

相关问题:如何使用 python(2.5 版)压缩文件夹的内容?

推荐答案

好的,在我明白你想要什么后,就像使用 zipfile.write 的第二个参数一样简单,你可以使用任何你想要的:

Ok, after i understood what you want, it is as simple as using the second argument of zipfile.write, where you can use whatever you want:

import zipfile
myZipFile = zipfile.ZipFile("zip.zip", "w" )
myZipFile.write("test.py", "dir\test.py", zipfile.ZIP_DEFLATED )

创建一个 zip 文件,其中 test.py 将被提取到一个名为 dir 的目录中

creates a zipfile where test.py would be extracted to a directory called dir

我曾经不得不在 zip 文件中创建一个空目录:这是可能的.上面的代码只是从zipfile中删除文件test.py后,文件消失了,但空目录仍然存在.

I once had to create an empty directory in a zip file: it is possible. after the code above just delete the file test.py from the zipfile, the file is gone, but the empty directory stays.

这篇关于使用python将文件夹添加到zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆