Getting files in a subdirectory in a zip

Scott David Daniels Scott.Daniels at Acm.Org
Mon Jul 11 13:12:59 EDT 2005


Echo wrote:
> What would the best way to get a list of files in a subdirectory in a zip?
> 
> The only thing I can come up with is to extract the zip into a temp
> directory and then just grab the list of files in the subdirectory
> that I need. This way seems very messy to me and I was wondering if
> there are any better ways.
> 

     import zipfile
     z = zipfile.ZipFile('block.zip')
     names = [name[7:] for name in z.namelist()
              if name.startswith('others/')]

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list