压缩单个文件陷入循环

宏化

我正在尝试压缩shapefile。我已经选择了LOCK和.CPG文件中shapefile栏的所有组件。由于某些原因,这些不能上传到我们的网站,并导致问题。

我尝试使用以下代码,但是它不断陷入循环并崩溃。我有一个单独的文件夹,可以使用shutil方法轻松将其压缩。但是,在尝试识别要压缩的文件时,我必须使用Zipfile模块。

rtc_shp = r"path/to/shp

zip = zipfile.ZipFile(os.path.join(datafolder, "Real_Time_Closures.zip"), "w")

for f in glob.glob(rtc_shp.replace(".shp",".*")):
    if not f.endswith(".lock"):
        if not f.endswith(".cpg"):
            zip.write(f, basename(f))

shutil.make_archive(indivfolder, "zip", indivfolder)
宏化

我通过创建一个空列表然后将文件名附加到列表中来绕过循环。然后,我将列表的内容写到了zip文件中。

zip = zipfile.ZipFile(os.path.join(datafolder, "MMO_Real_Time_Closures.zip"), "w", zipfile.ZIP_DEFLATED)

shp_zip_list = []

for f in glob.glob(rtc_shp.replace(".shp",".*")):
    if not f.endswith(".lock"):
        if not f.endswith(".cpg"):
            shp_zip_list.append(f)

for f in shp_list:
    zip.write(f, basename(f))

zip.close()

shutil.make_archive(indivfolder, "zip", indivfolder)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章