Open a tar archive for reading, writing or appending. Return an appropriate TarFile class. mode: 'r' or 'r:*' open for reading with transparent compression 'r:' open for reading exclusively uncompressed 'r:gz' open for reading wit
(cls, name=None, mode="r", fileobj=None, bufsize=RECORDSIZE, **kwargs)
| 1859 | |
| 1860 | @classmethod |
| 1861 | def open(cls, name=None, mode="r", fileobj=None, bufsize=RECORDSIZE, **kwargs): |
| 1862 | """Open a tar archive for reading, writing or appending. Return |
| 1863 | an appropriate TarFile class. |
| 1864 | |
| 1865 | mode: |
| 1866 | 'r' or 'r:*' open for reading with transparent compression |
| 1867 | 'r:' open for reading exclusively uncompressed |
| 1868 | 'r:gz' open for reading with gzip compression |
| 1869 | 'r:bz2' open for reading with bzip2 compression |
| 1870 | 'r:xz' open for reading with lzma compression |
| 1871 | 'r:zst' open for reading with zstd compression |
| 1872 | 'a' or 'a:' open for appending, creating the file if necessary |
| 1873 | 'w' or 'w:' open for writing without compression |
| 1874 | 'w:gz' open for writing with gzip compression |
| 1875 | 'w:bz2' open for writing with bzip2 compression |
| 1876 | 'w:xz' open for writing with lzma compression |
| 1877 | 'w:zst' open for writing with zstd compression |
| 1878 | |
| 1879 | 'x' or 'x:' create a tarfile exclusively without compression, raise |
| 1880 | an exception if the file is already created |
| 1881 | 'x:gz' create a gzip compressed tarfile, raise an exception |
| 1882 | if the file is already created |
| 1883 | 'x:bz2' create a bzip2 compressed tarfile, raise an exception |
| 1884 | if the file is already created |
| 1885 | 'x:xz' create an lzma compressed tarfile, raise an exception |
| 1886 | if the file is already created |
| 1887 | 'x:zst' create a zstd compressed tarfile, raise an exception |
| 1888 | if the file is already created |
| 1889 | |
| 1890 | 'r|*' open a stream of tar blocks with transparent compression |
| 1891 | 'r|' open an uncompressed stream of tar blocks for reading |
| 1892 | 'r|gz' open a gzip compressed stream of tar blocks |
| 1893 | 'r|bz2' open a bzip2 compressed stream of tar blocks |
| 1894 | 'r|xz' open an lzma compressed stream of tar blocks |
| 1895 | 'r|zst' open a zstd compressed stream of tar blocks |
| 1896 | 'w|' open an uncompressed stream for writing |
| 1897 | 'w|gz' open a gzip compressed stream for writing |
| 1898 | 'w|bz2' open a bzip2 compressed stream for writing |
| 1899 | 'w|xz' open an lzma compressed stream for writing |
| 1900 | 'w|zst' open a zstd compressed stream for writing |
| 1901 | """ |
| 1902 | |
| 1903 | if not name and not fileobj: |
| 1904 | raise ValueError("nothing to open") |
| 1905 | |
| 1906 | if mode in ("r", "r:*"): |
| 1907 | # Find out which *open() is appropriate for opening the file. |
| 1908 | def not_compressed(comptype): |
| 1909 | return cls.OPEN_METH[comptype] == 'taropen' |
| 1910 | error_msgs = [] |
| 1911 | for comptype in sorted(cls.OPEN_METH, key=not_compressed): |
| 1912 | func = getattr(cls, cls.OPEN_METH[comptype]) |
| 1913 | if fileobj is not None: |
| 1914 | saved_pos = fileobj.tell() |
| 1915 | try: |
| 1916 | return func(name, "r", fileobj, **kwargs) |
| 1917 | except (ReadError, CompressionError) as e: |
| 1918 | error_msgs.append(f'- method {comptype}: {e!r}') |