Search code examples
pythonpython-3.xtarfile

Python 3.6, Windows 10 .tar.gz extraction not working


I have tried a couple of different spins on in it, but do not appear to be getting any results. Below are two attempts at extracting the contents of a .tar.gz archive to the same folder where the compressed archive is;

For example I would start with just test_file.tar.gz in my Windows folder and end up with test_file.tar.gz, plus it's extracted contents.

The print statements show the code looping through the archive correctly in the first method, however I cannot see any extracted contents.

What am I doing wrong?

import tarfile
import os
import sys

    full_path = 'C:\Myfolder\test_file.tar.gz'

    if (full_path.endswith("tar.gz")):
        tar = tarfile.open(full_path, "r:gz")

        for item in tar:
            print(item)
            tar.extract(item)
        tar.close()


        with tarfile.open(full_path) as tar:
            print(full_path)
            tar.extractall()        
            tar.close()

Solution

  • I checked your code, everything is normal except indent...

    But please check your folder where you execute script or change

    tar.extractall('c:\\Path\\To\\Extract')