Try/Except to handle file write EOF errors

473 Views Asked by At

I have hundreds of .tar.gz files that come in a landing zone. The below python snippet runs on a schedule to extract these files and write the contents in another directory.

import tarfile
for f in files: 
   with tarfile.open(f) as uncompressed_file:
      uncompressed_file.extractall(outfile_path)

I receive the following error for some files, but it stops the remaining files from being processed.

EOFError: Compressed file ended before the end-of-stream marker was reached

Is there an try/except block I can use that will let me skip the error files and proceed to extract the remaining files?

3

There are 3 best solutions below

0
Tanner On

This will catch your error:

try:
    # your code
    
except EOFError as e:
    print(e)
0
Harsh Narwariya On

Is there an try/except block I can use that will let me skip the error files and proceed to extract the remaining files?

Yes, You can except EOFError in python.

try:
    # your code here
except EOFError as error:
    pass

But in your case, the archived file is corrupted thus handling of EOFError would not help that way to extract the next archives.

If you can provide link to your file It might help to get to the solution.

0
Sören On

If you get EOF before all files can be extracted, your archive is corrupted. No amount of try-catch can help you there, the damage is already done.

There are no "remaining files", because you're already at the end of the archive.