Possible Duplicate:
How do I zip the contents of a folder using python (version 2.5)?
假设我有一个目录:/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
您可以尝试使用distutils包:
注意,这不包括空目录。如果需要的话,可以在web上找到解决方法;最好是在我们最喜欢的归档程序中获取空目录的ZipInfo记录,查看其中的内容。
硬编码文件/路径,以摆脱我的代码细节。。。
您可以使用
subprocess
模块:这些代码没有经过测试,只是假装在unix机器上工作,我不知道windows是否有类似的命令行实用程序。
相关问题 更多 >
编程相关推荐