1

我有一个包含媒体文件的 zip 文件,在我提取这些文件并将路径附加到我的数据库之前,我需要清理路径,并提取 zip。

zip 中的路径列表包含

/no-fixed-name/no-fixed-name/images/2012/03/image.jpg
/no-fixed-name/no-fixed-name/images/2012/03/any-image.jpg
/no-fixed-name/no-fixed-name/videos/2012/03/video.mp4

我想去掉第一个两个目录的路径并得到这个

/images/2012/03/image.jpg
/images/2012/03/any-image.jpg

并加入"http://my-cdn-path.com/"每条路径

到目前为止,我已经这样做了,但无法将其剥离。

import os
import zipfile
import fnmatch

zf = zipfile.ZipFile('samplezip.zip','r')

a = zf.namelist()
search = '*.png'
searchresult = fnmatch.filter(a, search)


for i in searchresult:
   yo = os.path.abspath(i).split(os.sep)[2]
   #This way I can get the dir name that I want to remove but not sure how to do that.
4

1 回答 1

2

您可以执行以下操作(将路径视为字符串):

'http://my-cdn-path.com/' + '/'.join(i.split('/')[-4:]))

或者,使用 os.path:

'http://my-cdn-path.com/' + '/'.join(os.path.abspath(i).split(os.sep)[-4:])
于 2013-08-07T21:02:33.007 回答