擅长:python、mysql、java
<p><code>ZipFile.write</code>有<a href="https://docs.python.org/2/library/zipfile.html#zipfile.ZipFile.write" rel="nofollow">an optional second parameter ^{<cd2>}</a>
这正是你想要的。你知道吗</p>
<pre><code>import zipfile,os
project='C:/Users/user1/Documents/work/filesToZip'
# prevent adding zip to itself if the old zip is left in the directory
zip_path = os.path.join(project,'jobs.zip')
if os.path.exists(zip_path):
os.unlink(zip_path);
dirlist = os.listdir(project)
zip_file = zipfile.ZipFile(zip_path, 'w')
for file_name in dirlist:
zip_file.write(os.path.join(project, file_name), file_name)
zip_file.close()
</code></pre>