0

I have a task where I need to extract '.tgz' file but I keep getting following error.

Code used:

tar_path = os.path.join("datasets/housing/housing.tgz")
tgz_open = tarfile.open(tar_path,'r')

tgz_open.extract(tgz_open,"datasets/housing")
tgz_open.close()

Error:

---------------------------------------------------------------------------
ReadError                                 Traceback (most recent call last)
Input In [24], in <module>
      1 tar_path = os.path.join("datasets/housing/housing.tgz")
----> 2 tgz_open = tarfile.open(tar_path,'r')
      4 tgz_open.extract(tgz_open,"datasets/housing")
      5 tgz_open.close()

File ~\Anaconda3\lib\tarfile.py:1616, in TarFile.open(cls, name, mode, fileobj, bufsize, **kwargs)
   1614                 fileobj.seek(saved_pos)
   1615             continue
-> 1616     raise ReadError("file could not be opened successfully")
   1618 elif ":" in mode:
   1619     filemode, comptype = mode.split(":", 1)

ReadError: file could not be opened successfully

I even tried solution given in I want to extract a .tgz file and extract any subdirectories that have files that are .tgz and .tar but no luck, still the issue persists.

I tried to extract the '.tgz' file but I ended up with ReadError.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • What happens if you manually use the `tar` command to open and extract the `.tgz` file? I mean, without using Python, just using shell commands, are you able to extract the .tgz file? – Gino Mempin Apr 01 '23 at 04:14
  • Files with a `tgz` extension **are not** tar files; they're gzip files which will decompress to a tar file. – Karl Knechtel Apr 01 '23 at 04:21
  • "I even tried solution given in" Did you try the code in the **question**? In particular, see the part where there is a `'r:gz'` parameter for `tarfile.open`? – Karl Knechtel Apr 01 '23 at 04:22

0 Answers0