简体   繁体   English

Try/Except 处理文件写入 EOF 错误

[英]Try/Except to handle file write EOF errors

I have hundreds of .tar.gz files that come in a landing zone.我在登陆区中有数百个.tar.gz文件。 The below python snippet runs on a schedule to extract these files and write the contents in another directory.以下 python 片段按计划运行以提取这些文件并将内容写入另一个目录。

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?有没有我可以使用的 try/except 块让我跳过错误文件并继续提取剩余文件?

Is there an try/except block I can use that will let me skip the error files and proceed to extract the remaining files?有没有我可以使用的 try/except 块让我跳过错误文件并继续提取剩余文件?

Yes , You can except EOFError in python.的,你可以排除 python 中的 EOFError。

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.但在您的情况下,存档文件已损坏,因此处理 EOFError 无助于以这种方式提取下一个存档。

If you can provide link to your file It might help to get to the solution.如果您可以提供指向您的文件的链接,它可能有助于找到解决方案。

If you get EOF before all files can be extracted, your archive is corrupted.如果您在提取所有文件之前收到 EOF,则您的存档已损坏。 No amount of try-catch can help you there, the damage is already done.再多的 try-catch 也帮不了你,损失已经造成了。

There are no "remaining files", because you're already at the end of the archive.没有“剩余文件”,因为您已经在存档的末尾。

This will catch your error:这将捕获您的错误:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM