Geospatial datasets are often made up of multiple files, which are often published in file formats such as zip or tar. The tar format does not contain a compression algorithm, it provides an optional GZIP compression option.
Python contains modules that specifically read and write in zip and tar formats. These are the zipfile and tarfile modules, respectively.
The following example will extract a zip file hancockzip, which is hancockshp、 hancock.SHX and HancockThe dbf file will be extracted.
import zipfileLet's create a tar file based on the above unzipped file. In this example, open a tar file and write data to it. The write mode used at the time of writing is w:gz, which indicates that the compression format gzip is used. Related ** is as follows.zip = open("hancock.zip", "rb")
zipshape = zipfile.zipfile(zip)
for filename in zipshape.namelist():
# print(filename)
out = open(filename, "wb")
out.write(zipshape.read(filename))
out.close()
import tarfileYou can use tarfileextractall() method to extract these files. First, use a tarfileopen() method to open the file, and then extract the file, the relevant ** is as follows:tar = tarfile.open("hancock.tar.gz", "w:gz")
tar.add("hancock.shp")
tar.add("hancock.shx")
tar.add("hancock.dbf")
tar.close()
print('tar.The gz file has been generated')
tar = tarfile.open("hancock.tar.gz", "r:gz")Gather knowledge to nourish you and me.allfile = tar.extractall()
tar.close()
print(allfile)