try except inside a with open

Ganesh Pal ganesh1pal at gmail.com
Sat Jul 21 05:43:26 EDT 2018


>
>
>
> (1) Since this function always returns True (if it returns at all), what
> is the point? There's no point checking the return result, since it's
> always true, so why bother returning anything?
>
>

If  I don't return anything from a function it returns None.   But would it
be better if for the  function  i.e modify_various_line(f) to  atleast catch
or handle exceptions and log it for debugging purpose

Example:

def modify_various_line(f):
    """ Try modifiying various line """
    try:
        f.write('0123456789abcdef')
        f.seek(5)     # Go to the 6th byte in the file
        print f.read(1)
        f.seek(-3, 2) # Go to the 3rd byte before the end
        print f.read(1)
        f.write('END')
    except IOError as e:
       logging.error("Error: got %s" , % (str(e)))



More information about the Python-list mailing list