可能重复:
How do I zip the contents of a folder using python (version 2.5)?更好的方式来压缩Python中的文件(使用单个命令压缩整个目录)?
假设我有一个目录:/home/user/files/
。这个目录有一堆文件:
/home/user/files/
-- test.py
-- config.py
我想使用Python中ZipFile
这个目录压缩。我需要loop through the directory and add these files recursively,还是可以通过目录名称和ZipFile类自动添加它下面的所有内容?
最后,我想有:
/home/user/files.zip (and inside my zip, I dont need to have a /files folder inside the zip:)
-- test.py
-- config.py
由于os.walk产生的目录的全部内容 - 做递归你 - 这似乎是一个微不足道的循环。你想优化什么?代码行?我不知道如何。时间?不可能 - Zip需要时间。你有什么问题? – 2010-08-31 18:56:50
我只想压缩一个可能有空文件夹的文件夹,而无需为Linux机器使用zip实用程序和子流程模块的单个命令执行的操作编写大量代码行。 – 2010-09-01 17:39:23