"ReadError: file could not be opened successfully" when opening '.tgz' with tarfile

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.

Upvotes: 0

Views: 15

Answers (0)

Related Questions