我在使用 zipfile.Zipfile() 函数时遇到问题。它正确压缩我的文件,但在输出 zip 文件中包含我不想要的额外文件夹。它确实将我想要的所有文件放入 .zip 中,但默认情况下似乎添加了写入 .zip 文件中的文件的最后几个目录。有什么办法可以排除这些文件夹吗?这是我的代码:
import arcpy, os
from os import path as p
import zipfile
arcpy.overwriteOutput = True
def ZipShapes(path, out_path):
arcpy.env.workspace = path
shapes = arcpy.ListFeatureClasses()
# iterate through list of shapefiles
for shape in shapes:
name = p.splitext(shape)[0]
print name
zip_path = p.join(out_path, name + '.zip')
zip = zipfile.ZipFile(zip_path, 'w')
zip.write(p.join(path,shape))
for f in arcpy.ListFiles('%s*' %name):
if not f.endswith('.shp'):
zip.write(p.join(path,f))
print 'All files written to %s' %zip_path
zip.close()
if __name__ == '__main__':
path …Run Code Online (Sandbox Code Playgroud)