<blockquote>
<p><strong>Possible Duplicate:</strong><br/>
<a href="https://stackoverflow.com/questions/296499/how-do-i-zip-the-contents-of-a-folder-using-python-version-2-5">How do I zip the contents of a folder using python (version 2.5)?</a> </p>
</blockquote>
<p>假设我有一个目录:<code>/home/user/files/</code>。这个目录有一堆文件:</p>
<pre><code>/home/user/files/
-- test.py
-- config.py
</code></pre>
<p>我想用python中的<code>ZipFile</code>压缩这个目录。我是否需要<a href="https://stackoverflow.com/questions/458436/adding-folders-to-a-zip-file-using-python">loop through the directory and add these files recursively</a>,或者是否有可能传递目录名,并且ZipFile类会自动添加目录下的所有内容?</p>
<p>最后,我希望:</p>
<pre><code>/home/user/files.zip (and inside my zip, I dont need to have a /files folder inside the zip:)
-- test.py
-- config.py
</code></pre>