來源自 這裡
Preface :
This module makes it possible to read and write tar archives, including those using gzip or bz2 compression. Use the zipfile module to read or write .zip files, or the higher-level functions in shutil. Some facts and figures :
Supported APIs :
You can use below API to return TarFile object :
- tarfile.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)
- class tarfile.TarFile
- tarfile.is_tarfile(name)
- class tarfile.TarFileCompat(filename, mode='r', compression=TAR_PLAIN)
- exception tarfile.TarError
- exception tarfile.ReadError
- exception tarfile.CompressionError
- exception tarfile.StreamError
- exception tarfile.ExtractError
- exception tarfile.HeaderError
Each of the following constants defines a tar archive format that the tarfile module is able to create. See section Supported tar formats for details.
- tarfile.USTAR_FORMAT
- tarfile.GNU_FORMAT
- tarfile.PAX_FORMAT
- tarfile.DEFAULT_FORMAT
The following variables are available on module level :
- tarfile.ENCODING
TarFile Objects :
New in version 2.7: Added support for the context manager protocol.
The TarFile object provides an interface to a tar archive. A tar archive is a sequence of blocks. An archive member (a stored file) is made up of a header block followed by data blocks. It is possible to store a file in a tar archive several times. Each archive member is represented by a TarInfo object, see TarInfo Objects for details.
A TarFile object can be used as a context manager in a with statement. It will automatically be closed when the block is completed. Please note that in the event of an exception an archive opened for writing will not be finalized; only the internally used file object will be closed. See the Examples section for a use case.
- class tarfile.TarFile(name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, ignore_zeros=False, encoding=ENCODING, errors=None, pax_headers=None, debug=0, errorlevel=0)
- TarFile.open(...)
- TarFile.getmember(name)
- TarFile.getmembers()
- TarFile.getnames()
- TarFile.list(verbose=True)
- TarFile.next()
- TarFile.extractall(path=".", members=None)
- TarFile.extract(member, path="")
- TarFile.extractfile(member)
- TarFile.add(name, arcname=None, recursive=True, exclude=None, filter=None)
- TarFile.addfile(tarinfo, fileobj=None)
- TarFile.gettarinfo(name=None, arcname=None, fileobj=None)
- TarFile.close()
- TarFile.pax_headers
Examples :
How to extract an entire tar archive to the current working directory :
How to extract a subset of a tar archive with TarFile.extractall() using a generator function instead of a list:
How to create an uncompressed tar archive from a list of filenames :
How to read a gzip compressed tar archive and display some member information :
How to create an archive and reset the user information using the filter parameter in TarFile.add() :
Supplement :
* TarInfo Objects
* Supported tar formats
* Unicode issues
Preface :
This module makes it possible to read and write tar archives, including those using gzip or bz2 compression. Use the zipfile module to read or write .zip files, or the higher-level functions in shutil. Some facts and figures :
Supported APIs :
You can use below API to return TarFile object :
- tarfile.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)
- class tarfile.TarFile
- tarfile.is_tarfile(name)
- class tarfile.TarFileCompat(filename, mode='r', compression=TAR_PLAIN)
- exception tarfile.TarError
- exception tarfile.ReadError
- exception tarfile.CompressionError
- exception tarfile.StreamError
- exception tarfile.ExtractError
- exception tarfile.HeaderError
Each of the following constants defines a tar archive format that the tarfile module is able to create. See section Supported tar formats for details.
- tarfile.USTAR_FORMAT
- tarfile.GNU_FORMAT
- tarfile.PAX_FORMAT
- tarfile.DEFAULT_FORMAT
The following variables are available on module level :
- tarfile.ENCODING
TarFile Objects :
New in version 2.7: Added support for the context manager protocol.
The TarFile object provides an interface to a tar archive. A tar archive is a sequence of blocks. An archive member (a stored file) is made up of a header block followed by data blocks. It is possible to store a file in a tar archive several times. Each archive member is represented by a TarInfo object, see TarInfo Objects for details.
A TarFile object can be used as a context manager in a with statement. It will automatically be closed when the block is completed. Please note that in the event of an exception an archive opened for writing will not be finalized; only the internally used file object will be closed. See the Examples section for a use case.
- class tarfile.TarFile(name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, ignore_zeros=False, encoding=ENCODING, errors=None, pax_headers=None, debug=0, errorlevel=0)
- TarFile.open(...)
- TarFile.getmember(name)
- TarFile.getmembers()
- TarFile.getnames()
- TarFile.list(verbose=True)
- TarFile.next()
- TarFile.extractall(path=".", members=None)
- TarFile.extract(member, path="")
- TarFile.extractfile(member)
- TarFile.add(name, arcname=None, recursive=True, exclude=None, filter=None)
- TarFile.addfile(tarinfo, fileobj=None)
- TarFile.gettarinfo(name=None, arcname=None, fileobj=None)
- TarFile.close()
- TarFile.pax_headers
Examples :
How to extract an entire tar archive to the current working directory :
How to extract a subset of a tar archive with TarFile.extractall() using a generator function instead of a list:
How to create an uncompressed tar archive from a list of filenames :
How to read a gzip compressed tar archive and display some member information :
How to create an archive and reset the user information using the filter parameter in TarFile.add() :
- import tarfile
- def reset(tarinfo):
- tarinfo.uid = tarinfo.gid = 0
- tarinfo.uname = tarinfo.gname = "root"
- return tarinfo
- tar = tarfile.open("sample.tar.gz", "w:gz")
- tar.add("foo", filter=reset)
- tar.close()
* TarInfo Objects
* Supported tar formats
* Unicode issues
This message was edited 42 times. Last update was at 11/06/2012 10:18:37
沒有留言:
張貼留言