Why won't it expand both tar.gz files?
Question: I have two tar.gz files, 2014_SRS.tar.gz and 2013_SRS.tar.gz. Each of the
files contains a folder called SRS, which is full of text files. I downloaded
these from an ftp server. I want to unzip them automatically in Python. This
is my code:

    
    
    import re
    import ftplib
    import os
    import time
    import tarfile
    import sys
    
    print('1')
    
    tar = tarfile.open('2014_SRS.tar.gz')
    tar.extractall()
    tar.close()
    
    print('2')
    
    tar = tarfile.open('2013_SRS.tar.gz')
    tar.extractall()
    tar.close()
    
    print('3')
    

This code only opens the second file. How do I fix it to open both files?

Also, I tried using a for loop to run through the whole directory. The code is
shown below.

    
    
    for i in os.listdir(os.getcwd()):
         if i.endswith(".tar.gz"): 
             tar = tarfile.open(i, "r:gz")
             tar.extractall()
             tar.close()
    

However this gave me an EOFError. In addition, before I ran bit of code, I was
able to unzip both files manually. However, after I run it, and after the code
gives me an error, I cannot unzip the 2014_SRS file manually anymore. How do I
fix this?


Answer: While this may not answer your specific question as to why both files could
not be unzipped with your code , the following is one way to unzip a list of
tar.gz files.

    
    
    import tarfile, glob
    
    srcDir = "/your/src/directory" 
    dstDir = "/your/dst/directory"
    
    for f in glob.glob(srcDir + "/*.gz"):
    
        t = tarfile.open(f,"r:gz")
    
        for member in t.getmembers():
                t.extract(member,dstDir)
        t.close()
    

