I Have a list of Landsat scenes. Inside of tar.gz are multiple bands. Like:
File " LC08018051date.tar.gz "
LC08_date_sr_band1.tif
LC08_date_sr_band2.tif
LC08_date_sr_band3.tif
LC08_date_sr_band4.tif
LC08_date_sr_band5.tif
LC08_date_sr_band6.tif
LC08_date_sr_band7.tif
I need to extract only LC08_date_sr_band3.tif
and LC08_date_sr_band4.tif
to calculate an index.
This code works extracting all "band" from all files.
import tarfile
import os
import glob
files = glob.glob('L*.tar.gz')
for fileName in files:
tfile = tarfile.open(fileName, 'r:gz')
membersList = tfile.getmembers()
namesList = tfile.getnames()
bandsList = [x for x, y in zip(membersList, namesList) if "sr_band" in y]
print("extracting...")
tfile.extractall("Folder/",members=bandsList)
print ("Done")
The thing is i need extract only sr_band3 and sr_band4
Not the best option but what i do is:
import tarfile
import os
import glob
import re
files = glob.glob('L*.tar.gz')
if not files:
raise Exception('No se han encontrado archivos a procesar')
for fileName in files:
tfile = tarfile.open(fileName, 'r:gz')
membersList = tfile.getmembers()
namesList = tfile.getnames()
bandsList = [x for x, y in zip(membersList, namesList) if "sr_band3" in y]
print("extracting...")
tfile.extractall("bands/",members=bandsList)
print ("Done")
for fileName in files:
tfile = tarfile.open(fileName, 'r:gz')
membersList = tfile.getmembers()
namesList = tfile.getnames()
bandsList = [x for x, y in zip(membersList, namesList) if "sr_band4" in y]
print("extracting...")
tfile.extractall("bands/",members=bandsList)
print ("Done")
The code enter each time to the file, so is not the fastest way.
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.