Click here to Skip to main content
14,271,891 members
Rate this:
Please Sign up or sign in to vote.
In python, I have a folder with many tar.gz files. I want to be able to get to one .data file in each tar.gz file and collect a line of data. How do I loop a folder and read contents in a file like this.

list =["C:\\user\\desktop\\test_folder\\arch126.tar\\arch126.tar.dir\\","gz:r","C:\\user\\desktop\\test_folder\\arch127.tar\\arch127.tar.dir\\","gz:r"]
for i in list:
open(i) as f:

FileNotFoundError [Errno 2] No such file or directory :

What I have tried:

tar ="C:\\user\\desktop\\test_folder\\arch126.tar\\arch126.tar.dir\\")
for member in tar.getmembers():
   F = tar.extractfile(member)
   if F is not None:
       content =

    import tarfile
    import os
    for i in os.listdir(r'C:\users\test_folder'):
       str = r'C:\users\test_folder'
       str1 = str1 + '\\' + i
       if str1.endwith(".tar.gz"):
          tar =,"r:*")
          for member in tar:
             f = tar.extractfile(member)
Richard MacCutchan 13-Aug-19 4:32am
You are trying to open a directory rather than a file.
mike_j7909 14-Aug-19 0:43am
correct was trying to loop over a folder which I believe is a director. correct? or can you suggest a better way.
Richard MacCutchan 14-Aug-19 3:15am
The problem is that somewhere (you did not tell us where) you are getting an error trying to access a file that does not exist. Check the place where the error occurs and see exactly what is the filename that you are trying to open.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100