Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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\\ter126.data","gz:r","C:\\user\\desktop\\test_folder\\arch127.tar\\arch127.tar.dir\\ter126.data","gz:r"]
for i in list:
open(i) as f:
print(f.read())

FileNotFoundError [Errno 2] No such file or directory :
"C:\\user\\desktop\\test_folder\\arch126.tar\\arch126.tar.dir\\ter126.data"

What I have tried:

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

<pre>
    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 = tarfile.open(str1,"r:*")
          for member in tar:
             f = tar.extractfile(member)
Posted
Comments
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900