我的档案结构是常规格式,pathtofile\folder.zip
,我需要访问每个档案中的文件格式为“ folder \ image.png”。
我用来实现此目的的代码是:
import zipfile
imgname = path[:-4].split('\\')[-1] + '\\image.png'
imgfile = archive.extract(imgname, path=dst)`
path = pathtofile\\folder.zip
但是这将引发以下错误:
KeyError: "There is no item named 'folder\\\\image.png' in the archive"
我也尝试过从\\
定义中删除imgname
,但这会引发以下错误:
KeyError: "There is no item named 'folderimage.png' in the archive"
所以反斜杠似乎确实在以某种方式重复了自己。
我想知道是什么导致反斜杠成倍增加;如果有人有任何明智的话,将不胜感激!
代码的较大部分:
archives = dict()
sub_data = data['file_path'] # data is a pandas dataframe containing file names and data about each file
filenames = get_unique(list(sub_data)) # get_unique is equivalent to list(set(sequence))
for filename in filenames:
zip_path = image_dir+filename+'.zip'
fil = sub_data == filename
frames = [f for f in data.loc[fil, 'local_index']]
archives.update({zip_path: frames})
keys = list(archives.keys())
for i in range(len(filenames)):
key = keys[i]
archive = zipfile.ZipFile(key, 'r')
dst = output_image_dir+'\\'+filenames[i] # output_image_dir is a filepath not ending in '\\'
if not os.path.exists(dst):
os.makedirs(dst)
for frame in archives[key]:
imgname = os.path.join(key[:-4].split('\\')[-1], 'frame'+str(frame).zfill(7)+'.png')
if not os.path.exists(dst+'\\'+imgname):
imgfile = archive.extract(imgname, path=dst)