我一直在使用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 = r'C:\Shape_test\Census_CedarCo'
out_path = r'C:\Shape_outputs'
ZipShapes(path, out_path)
我試圖張貼一些照片,但我沒有足夠的聲望點數。基本上它是在zip文件中添加2個額外的文件夾(空的)。因此,而不是文件作爲拉鍊像這裏面:
C:\Shape_outputs\Public_Buildings.zip\Public_Buildings.shp
他們表現出了這樣的:
C:\Shape_outputs\Public_Buildings.zip\Shape_test\Census_CedarCo\Public_Buildings.shp
的「Shape_test」和「Census_CedarCo」文件夾,我的形狀文件目錄試圖複製來自,但如果我只是寫這些文件,爲什麼子目錄也被複制到zip文件中?我想這不是一個大問題,因爲我正在獲取壓縮文件,但這比任何事情都更令人厭煩。
我認爲當創建一個zip文件時,它只會寫入我自己指定的文件。爲什麼它會在zip文件中添加這些額外的目錄?有沒有辦法解決它?我在這裏錯過了什麼嗎?我感謝任何輸入!謝謝
美麗!我想知道arcname參數是什麼......我想我應該多做一些閱讀。現在完美的工作!再次感謝! – cmackey