correct way to catch exception with Python 'with' statement

Michael Torrie torriem at gmail.com
Mon Nov 28 10:59:11 EST 2016


On 11/28/2016 08:18 AM, Ganesh Pal wrote:
> On Mon, Nov 28, 2016 at 1:16 PM, Steven D'Aprano <
> steve+comp.lang.python at pearwood.info> wrote:
> 
>>
>>
>> There is no need to return True. The function either succeeds, or it
>> raises an
>> exception, so there is no need to return any value at all.
>>
>>
>  I returned True here ,because based on the result of this function , I
> would want to perform next steps

Except that you didn't read what Steven wrote, and you may not be
understanding your own code.  If the function fails it never returns
anyway, since an exception is raised!  So you don't need to worry about
a return type at all. You just do:

create_files_append()
do_something().

If you want to check for the exception, you would do:
try:
	create_files_append()
	do_something()
except OSError:
	do_next_thing()





More information about the Python-list mailing list