dkovacs
dkovacs

Reputation: 39

Try/Except to handle file write EOF errors

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?

Upvotes: 1

Views: 635

Answers (3)

Harsh Narwariya
Harsh Narwariya

Reputation: 565

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.

Upvotes: 1

Sören
Sören

Reputation: 2433

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.

Upvotes: 2

Tanner
Tanner

Reputation: 181

This will catch your error:

try:
    # your code
    
except EOFError as e:
    print(e)

Upvotes: 0

Related Questions