Python:如何写入存档中的现有文件夹?

Python:如何写入存档中的现有文件夹?,python,zip,archive,Python,Zip,Archive,我正在尝试写入包含现有文件夹的zip存档。 到目前为止,我只能写:x.write(newfiles)这给了我dir1/dir2/dir…n/newfiles或者x.write(newfiles,os.path.rel(newfiles,root)),它将我存档中的所有新文件都放在任何子目录之外。然而,我需要完成的是将我的新文件写入存档中已存在的子目录。 请帮忙!谢谢! 我的代码: #create a list of the png files to be added img_lib = [] f


我正在尝试写入包含现有文件夹的zip存档。 到目前为止,我只能写:

x.write(newfiles)
这给了我
dir1/dir2/dir…n/newfiles


或者

x.write(newfiles,os.path.rel(newfiles,root))
,它将我存档中的所有新文件都放在任何子目录之外。


然而,我需要完成的是将我的新文件写入存档中已存在的子目录。


请帮忙!谢谢!

我的代码:

#create a list of the png files to be added
img_lib = []
for root, dirs, files in os.walk(img_dir):
    for i in files:
        img_lib.append(root + '/' + i)

#open kmz archive for editing in 'append' mode
kmzarc = zipfile.ZipFile(TCP_kmz, mode = 'a')

#loop through list of available png files in img_lib and add to archive
for i in img_lib:
    kmzarc.write(i, 'images/'+os.path.relpath(i, root))

#close file
kmzarc.close()

更新:我已经能够通过x.write(newfiles,'newfolder/'+os.path.rel(newfiles,root))将一个包含我所有新文件的新文件夹添加到存档中。但是,这将删除存档中的所有其他现有文件夹。