Python - Extracting ZIP

import zipfile
import os
def unzipfile(source_path, destination_path, zipfile_name):
    f = zipfile.ZipFile(source_path, 'r')
    print('Extracting {}...'.format(zipfile_name))
    f.extractall(destination_path)
    f.close()
    print('{} has been extracted.'.format(zipfile_name))
train_path = 'E:/ai_challenger/scene classification/dataset/ai_challenger_scene_train_20170904.zip'
validation_path = 'E:/ai_challenger/scene classification/dataset/ai_challenger_scene_validation_20170908.zip'
test_a_path = 'E:/ai_challenger/scene classification/dataset/ai_challenger_scene_test_a_20170922.zip'
destination_path = 'E:/ai_challenger/scene classification/dataset'

unzipfile(train_path, destination_path, "train_data")
unzipfile(validation_path, destination_path, "validation_data")
unzipfile(test_a_path, destination_path, "test_a_data")
Extracting train_data...
train_data has been extracted.
Extracting validation_data...
validation_data has been extracted.
Extracting test_a_data...
test_a_data has been extracted.

Zip format and Python ZipFile class

class zipfile.ZipFile(file, mode=’r’, compression=ZIP_STORED, allowZip64=True)

file:

can be a path to a file (a string), a file-like object or a path-like object.

4 ways to route Windows path in Python

'E:\\ai_challenger\\scene classification\\dataset\\ai_challenger_scene_train_20170904.zip'
'E:/ai_challenger/scene classification/dataset/ai_challenger_scene_train_20170904.zip'
r'E:\ai_challenger\scene classification\dataset\ai_challenger_scene_train_20170904.zip'
import os
a = 'E:'
b = 'ai_challenger' 
c = 'scene classification'
d = 'dataset'
e = 'ai_challenger_scene_train_20170904.zip'
test_a_path = os.path.join(a + os.sep, b, c, d, e)
# The character used by the operating system to separate pathname components. 
# This is '/' for POSIX and '\\' for Windows.
print(test_a_path)
E:\ai_challenger\scene classification\dataset\ai_challenger_scene_test_a_20170922.zip

mode:

  • 'r' to read an existing file, in example above we just read existing file in the folder.
  • 'w' to truncate and write a new file
  • 'a' to append to an existing file
  • 'x' to exclusively create and write a new file

compression:

compression is the ZIP compression method to use when writing the archive, and should be ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2 or ZIP_LZMA; unrecognized values will cause NotImplementedError to be raised

allowZip64:

If allowZip64 is True (the default) zipfile will create ZIP files that use the ZIP64 extensions when the zipfile is larger than 4 GiB. If it is false zipfile will raise an exception when the ZIP file would require ZIP64 extensions.


Extraction

ZipFile.extractall(path=None, members=None, pwd=None)
Extract all members from the archive to the current working directory. path specifies a different directory to extract to. members is optional and must be a subset of the list returned by namelist(). pwd is the password used for encrypted files.

(TAR, GZ and other archives to be continued.)

你可能感兴趣的:(Python - Extracting ZIP)