(self, file, mode="r", compression=ZIP_STORED, allowZip64=True,
                 compresslevel=None, *, strict_timestamps=True)
Open the ZIP file with mode read 'r', write 'w', exclusive create 'x',
        or append 'a'.
Open the ZIP file with mode read 'r', write 'w', exclusive create 'x',
        or append 'a'.
Open the ZIP file with mode read r write w exclusive create x or append a .
def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True,
                 compresslevel=None, *, strict_timestamps=True):
        """Open the ZIP file with mode read 'r', write 'w', exclusive create 'x',
        or append 'a'."""
        if mode not in ('r', 'w', 'x', 'a'):
            raise ValueError("ZipFile requires mode 'r', 'w', 'x', or 'a'")

        _check_compression(compression)

        self._allowZip64 = allowZip64
        self._didModify = False
        self.debug = 0  # Level of printing: 0 through 3
        self.NameToInfo = {}    # Find file info given name
        self.filelist = []      # List of ZipInfo instances for archive
        self.compression = compression  # Method of compression
        self.compresslevel = compresslevel
        self.mode = mode
        self.pwd = None
        self._comment = b''
        self._strict_timestamps = strict_timestamps

        # Check if we were passed a file-like object
        if isinstance(file, os.PathLike):
            file = os.fspath(file)
        if isinstance(file, str):
            # No, it's a filename
            self._filePassed = 0
            self.filename = file
            modeDict = {'r' : 'rb', 'w': 'w+b', 'x': 'x+b', 'a' : 'r+b',
                        'r+b': 'w+b', 'w+b': 'wb', 'x+b': 'xb'}
            filemode = modeDict[mode]
            while True:
                try:
                    self.fp = io.open(file, filemode)
                except OSError:
                    if filemode in modeDict:
                        filemode = modeDict[filemode]
                        continue
                    raise
                break
        else:
            self._filePassed = 1
            self.fp = file
            self.filename = getattr(file, 'name', None)
        self._fileRefCnt = 1
        self._lock = threading.RLock()
        self._seekable = True
        self._writing = False

        try:
            if mode == 'r':
                self._RealGetContents()
            elif mode in ('w', 'x'):
                # set the modified flag so central directory gets written
                # even if no files are added to the archive
                self._didModify = True
                try:
                    self.start_dir = self.fp.tell()
                except (AttributeError, OSError):
                    self.fp = _Tellable(self.fp)
                    self.start_dir = 0
                    self._seekable = False
                else:
                    # Some file-like objects can provide tell() but not seek()
                    try:
                        self.fp.seek(self.start_dir)
                    except (AttributeError, OSError):
                        self._seekable = False
            elif mode == 'a':
                try:
                    # See if file is a zip file
                    self._RealGetContents()
                    # seek to start of directory and overwrite
                    self.fp.seek(self.start_dir)
                except BadZipFile:
                    # file is not a zip file, just append
                    self.fp.seek(0, 2)

                    # set the modified flag so central directory gets written
                    # even if no files are added to the archive
                    self._didModify = True
                    self.start_dir = self.fp.tell()
            else:
                raise ValueError("Mode must be 'r', 'w', 'x', or 'a'")
        except:
            fp = self.fp
            self.fp = None
            self._fpclose(fp)
            raise
def __init__ ( self , file , mode = "r" , compression = ZIP_STORED , allowZip64 = True , compresslevel = None , * , strict_timestamps = True ) : if mode not in ( 'r' , 'w' , 'x' , 'a' ) : raise ValueError ( "ZipFile requires mode 'r', 'w', 'x', or 'a'" ) _check_compression ( compression ) self . _allowZip64 = allowZip64 self . _didModify = False self . debug = 0 # Level of printing: 0 through 3 self . NameToInfo = { } # Find file info given name self . filelist = [ ] # List of ZipInfo instances for archive self . compression = compression # Method of compression self . compresslevel = compresslevel self . mode = mode self . pwd = None self . _comment = b'' self . _strict_timestamps = strict_timestamps # Check if we were passed a file-like object if isinstance ( file , os . PathLike ) : file = os . fspath ( file ) if isinstance ( file , str ) : # No, it's a filename self . _filePassed = 0 self . filename = file modeDict = { 'r' : 'rb' , 'w' : 'w+b' , 'x' : 'x+b' , 'a' : 'r+b' , 'r+b' : 'w+b' , 'w+b' : 'wb' , 'x+b' : 'xb' } filemode = modeDict [ mode ] while True : try : self . fp = io . open ( file , filemode ) except OSError : if filemode in modeDict : filemode = modeDict [ filemode ] continue raise break else : self . _filePassed = 1 self . fp = file self . filename = getattr ( file , 'name' , None ) self . _fileRefCnt = 1 self . _lock = threading . RLock ( ) self . _seekable = True self . _writing = False try : if mode == 'r' : self . _RealGetContents ( ) elif mode in ( 'w' , 'x' ) : # set the modified flag so central directory gets written # even if no files are added to the archive self . _didModify = True try : self . start_dir = self . fp . tell ( ) except ( AttributeError , OSError ) : self . fp = _Tellable ( self . fp ) self . start_dir = 0 self . _seekable = False else : # Some file-like objects can provide tell() but not seek() try : self . fp . seek ( self . start_dir ) except ( AttributeError , OSError ) : self . _seekable = False elif mode == 'a' : try : # See if file is a zip file self . _RealGetContents ( ) # seek to start of directory and overwrite self . fp . seek ( self . start_dir ) except BadZipFile : # file is not a zip file, just append self . fp . seek ( 0 , 2 ) # set the modified flag so central directory gets written # even if no files are added to the archive self . _didModify = True self . start_dir = self . fp . tell ( ) else : raise ValueError ( "Mode must be 'r', 'w', 'x', or 'a'" ) except : fp = self . fp self . fp = None self . _fpclose ( fp ) raise