'Uncompress batch of "tar.gz" files in a folder

There is a folder which contain several "tar.gz" files. I want to uncompress these files. I write python code below. But it reports error: [Errno 2] No such file or directory: 'DT_20180322.tar.gz'. I wonder why I have this error and how to correct it? Thanks!

import tarfile
files = [f for f in os.listdir('TE-xy/')]
for fname in files: 
    if fname.endswith("tar.gz"):
        tar = tarfile.open(fname, "r:gz")
        tar.extractall()
        tar.close()


Solution 1:[1]

The problem is you are just using the filename.

You need to include the directory name as well.

import os
import tarfile

files = [f for f in os.listdir('TE-xy/')]
for fname in files: 
    if fname.endswith("tar.gz"):
        fpath = os.path.join('TE-xy', fname)
        tar = tarfile.open(fpath, "r:gz")
        tar.extractall()
        tar.close()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ewong