How to skip action when an exception occurs in the with statement

Asked 1 months ago, Updated 1 months ago, 5 views

If you try to extract a compressed file from the address stored in the variable local with the following code, and if there is no compressed file called local and a FileNotFoundError occurs, I would like to skip the processing for local.
How can I change the code below to solve the problem above...?

Code

#coding:utf-8
import zipfile

local = 'local.zip'
with zipfile.ZipFile(local, 'r') as zf: Read the #zip file.How do I handle exceptions when I can't find a file...
    files=[ info.filename for info in zf.infolist() if info.filename.endswith('.txt') ]

python python3 exception

2022-09-30 11:35

1 Answers

WithOut of the block when certain exceptions occur within the block

You can use contextlib.suppress.

#!/usr/bin/python3
import zipfile
from contextlib import suppress

with suppress (FileNotFoundError):
    with zipfile.ZipFile("file.zip", 'r') as zf:
        print("success:",zf)#some action

These nested with statements can be combined to reduce indentation.

 with suppress (FileNotFoundError), zipfile.ZipFile("file.zip", 'r') as zf:
    print("success:",zf)#some action

Catch only exceptions to context expressions

On the , we have captured all exceptions (FileNotFoundError) that occurred within the with block.Instead, you can use contextlib.ExitStack to handle only exceptions in the context expression (where you open the file with zipfile.ZipFile in this example).

 from contextlib import ExitStack
# approximately
# Escape with continue from block as end of for statement
with ExitStack() as stack:
    try:
        zf = stack.enter_context(zipfile.ZipFile("file.zip", 'r')
    except FileNotFoundError:
        continue
    print("success:",zf)#some action

Get out of the block anywhere

Above, the with statement should be at the end of the for statement, and continue omits the with statement.For example, you can use return at the end of a function.However, in some cases, the with sentence is not placed in such a place.In that case, I think it would be good to combine the contextlib.suppress introduced above.

#!/usr/bin/python3
import zipfile
from contextlib import suppress, ExitStack

class SkipError (Exception):
    pass

with suppress(SkipError), ExitStack() as stack:
    try:
        zf = stack.enter_context(zipfile.ZipFile("file.zip", 'r')
    except FileNotFoundError:
        raise SkipError ("zip file not found")

    print("success:",zf)#some action

Other references include "Catch exceptions from the __enter__ method" in the official documentation.


2022-09-30 11:35

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.