简体   繁体   中英

Untar files with python and extract certain files with tarfile.extractall() python

I am trying to untar files with tarfile.extractall() so that I can get a subset of the images in the directory by using the optional parameter 'members'.

I have an array containing the filenames of all the files that I would like to extract that I am passing in here but from what I understand they should be TarInfo objects. How can I turn my array of file names into an array of TarInfo objects? Or is there a better way to do this?

t = tarfile.open(filename, 'r')
t.extractall(out_dir, filearray)

If it is a requirement to use extractall , you first need to generate a list of tarinfo objects that match with filearray

members = [ x for x in t.getmembers() if x.name in filearray]

You can then pass that into extractall

t.extractall(out_dir, members)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM