Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #196379

Re: Best use of "open" context manager

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Alan Gauld <learn2program@gmail.com>
Newsgroups comp.lang.python
Subject Re: Best use of "open" context manager
Date Sat, 6 Jul 2024 12:43:40 +0100
Lines 88
Message-ID <mailman.5.1720266224.2981.python-list@python.org> (permalink)
References <954c4ca8-cf37-4482-a1be-46d39cb503f9@btinternet.com> <05efb266-d63a-4965-8806-8733bb1ff734@yahoo.co.uk>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de CunRFDqtJMTDCPmCWFbOSw4HfDFP5SHZFpzQac3YVn2A==
Cancel-Lock sha1:zJwYhFtvsUOEEyhevcSisgaD4fQ= sha256:bg4/J+CDkoMnQY02qBwq1dhcK3dU/HOivjbPTzJqxBU=
Return-Path <learn2program@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
Authentication-Results mail.python.org; dkim=pass reason="2048-bit key; unprotected key" header.d=gmail.com header.i=@gmail.com header.b=X6lFdyMa; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status OK 0.097
X-Spam-Evidence '*H*': 0.81; '*S*': 0.00; 'this:': 0.03; 'issue.': 0.05; 'e.g.': 0.07; 'action,': 0.09; 'else:': 0.09; 'readable': 0.09; 'memory': 0.15; 'applies': 0.16; 'decreases': 0.16; 'easily.': 0.16; 'flickr': 0.16; 'from:name:alan gauld': 0.16; 'message-id:@yahoo.co.uk': 0.16; 'outer': 0.16; 'paths': 0.16; 'photo-blog': 0.16; 'preferable': 0.16; 'pythonic': 0.16; 'url- ip:79.170.44.132/32': 0.16; 'url-ip:79.170.44/24': 0.16; 'url- ip:79.170/16': 0.16; 'url-ip:79/8': 0.16; 'url:alan-g': 0.16; 'url:alan_gauld': 0.16; 'url:alangauldphotos': 0.16; 'wrote:': 0.16; 'url:amazon': 0.19; 'to:addr:python-list': 0.20; 'option': 0.20; 'version': 0.23; 'code': 0.23; 'lines': 0.23; 'to:name :python-list@python.org': 0.24; '(and': 0.25; 'so.': 0.26; 'error': 0.29; 'header:User-Agent:1': 0.30; 'program': 0.31; 'think': 0.32; 'concern': 0.32; 'python-list': 0.32; 'received:192.168.1': 0.32; 'but': 0.32; 'there': 0.33; 'core': 0.34; 'header:In-Reply-To:1': 0.34; 'received:google.com': 0.34; 'handling': 0.35; 'close': 0.35; 'from:addr:gmail.com': 0.35; 'errors': 0.36; 'missing': 0.37; 'really': 0.37; 'using': 0.37; "it's": 0.37; 'author': 0.37; 'others': 0.37; 'received:192.168': 0.37; 'file': 0.38; 'way': 0.38; 'could': 0.38; 'put': 0.38; 'read': 0.38; 'single': 0.39; 'valid': 0.39; 'use': 0.39; 'block': 0.39; 'still': 0.40; 'advantage': 0.40; 'alan': 0.40; 'program.': 0.40; 'learn': 0.40; 'something': 0.40; 'want': 0.40; 'try': 0.40; 'should': 0.40; 'best': 0.61; 'kept': 0.61; 'remember': 0.61; 'skip:o 10': 0.61; 'here': 0.62; 'follow': 0.62; 'finished': 0.64; 'less': 0.65; 'entire': 0.67; 'url-ip:18/8': 0.67; 'operations': 0.68; 'safer': 0.69; 'small,': 0.69; 'url:author': 0.69; 'below': 0.69; 'site': 0.70; 'compared': 0.71; 'ignore': 0.71; 'suite': 0.71; 'extra': 0.84; 'catching': 0.84; 'clause': 0.84; 'dealt': 0.84; 'forgot': 0.84; 'inside.': 0.84; 'massive': 0.84; 'mentally': 0.84; 'presented.': 0.84; 'readability.': 0.84; 'rob': 0.84; 'statement.': 0.84; 'subject:manager': 0.84; 'subject:open': 0.84; 'violates': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720266222; x=1720871022; darn=python.org; h=content-transfer-encoding:in-reply-to:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=ivbyxAIShRH7w6MSYHmLYFLJ4C9/wUqHHwLNOePDW4Y=; b=X6lFdyMaYX+dhX8IGMHVCkHbvYxyKQr+5xcDRn/P6rK1nMemL/DbBFy92wcW3TAjg5 evXTzubB/gyfyPt78BCA1fTd5ruX8euGrLvWhWuNQJcgzphiFrZAlHE8tGi6YOGK8HkV n/BS3WkCSdIcRbRZF0BIJ4GihJdxkaIoH+MP9l/fsdO9zpN5LMF4/5Lvk/KhsHjEpi6/ 9Ogn2EtDjfFej8O8ZlX63JOUShKKKoRHUIAgAw/SWAMDDtZGx3irjm/HcT25BpoyxqO5 TRIL1fZR6ApDaloNCXMz2pu0jIymqcjz/UNcKENTyr/3s/McgUcUSrLSD8TlMnaK2hDb pYdw==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720266222; x=1720871022; h=content-transfer-encoding:in-reply-to:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ivbyxAIShRH7w6MSYHmLYFLJ4C9/wUqHHwLNOePDW4Y=; b=btDYOg7PDW3GFqxLqxjuKWkVlxe0DN6pZXKYTXfrwTDBhVrtoCf+tWnNWQkcW96gxn +PpMfJqbAWysMbzc+eoKuUB+H+t24BHidQEDIBxeYVVbd2C/HJCiA5VrgkdXIbBsCskp joSMd8c751I3xhW0wjtfGJcoAylhKjVF3LGXBvEUltL9ot3rvT4mTIY6wH+YRgumpllY hnZDV2noqsilv1Q/1idL8SMwYQqoVcoCbqFRZ8Jo3QmrlkdPUZzAreT1uraL4Ia+QAsX 9I62THXLIRhtFGxnLoXPYDTsya9PGAaz1OzcovJoeZKfOknUknCk+fUhgXtmdNrX14Fk FrrQ==
X-Forwarded-Encrypted i=1; AJvYcCUYtLzkJPeaDn7o7gEOtd1PvO1q+eKMU1bEM72P+sHMDWr8hjBVP4Ap6L5HnAq7YegJmSJ31qkN/+aEpOgC2s3Et6aLZntF
X-Gm-Message-State AOJu0YyXEYa/5NQJNhPJBtjjlexXUxTQtgAr/m2iZThSvjzGIIIE4X5k 1aMD7Rc2XGeVWDESGQu1IeKl2sxS60USIHDPzLbxQ09E6IJrPY5A
X-Google-Smtp-Source AGHT+IES7cJgmRNy6lrF1fbALH7KorIJFmWTAJHgSWrAdmIXcQCsKd4v14pfHYfsaSXnSbADbg1MDQ==
X-Received by 2002:adf:fa92:0:b0:360:866f:5083 with SMTP id ffacd0b85a97d-3679f739c45mr6113251f8f.32.1720266222247; Sat, 06 Jul 2024 04:43:42 -0700 (PDT)
X-Google-Original-From Alan Gauld <alan.gauld@yahoo.co.uk>
User-Agent Mozilla Thunderbird
Content-Language en-GB
In-Reply-To <954c4ca8-cf37-4482-a1be-46d39cb503f9@btinternet.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.39
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <https://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <05efb266-d63a-4965-8806-8733bb1ff734@yahoo.co.uk>
X-Mailman-Original-References <954c4ca8-cf37-4482-a1be-46d39cb503f9@btinternet.com>
Xref csiph.com comp.lang.python:196379

Show key headers only | View raw


On 06/07/2024 11:49, Rob Cliffe via Python-list wrote:

>      If the file does not exist I want to take appropriate action, e.g. 
> print an error message and abort the program.
> I might write it like this:
> 
> try:
>      with open(FileName) as f:
>          for ln in f:
>              print("I do a lot of processing here")
>              # Many lines of code here .....
> except FileNotFoundError:
>      print(f"File {FileName} not found")
>      sys.exit()
> 
> but this violates the principle that a "try" suite should be kept small, 

The try is small, it only has a single statement inside.
The compound block inside that statement should have its
own try/ecxepts but the outer one really only applies to
the with statement.

I certainly prefer this option to any of the others presented.

> not to mention that having "try" and "except" far apart decreases 
> readability.

This is a valid concern although that's part of the reason
we use indentation. Compared to early BASIC and FORTRAN with massive
GOSUB type leaps (and often no indentation) it's very readable!

But its still preferable to having the multi-level indents below
or having to remember to close the file when finished (and
ensure that all possible paths do so.

> try:
>      f = open(FileName) as f:
>      FileLines = f.readlines()
> except FileNotFoundError:
>      print(f"File {FileName} not found")
>      sys.exit()
> # I forgot to put "f.close()" here -:)

Exactly! That's why using with is safer even if the
except is detached from the try.

You are also reading the entire file into memory which could
be an issue. And you are not catching any errors in the
read operations because the except only covers a missing
file.

> Really I would like to write something like
> 
> try:
>      with open(FileName) as f:
> except FileNotFoundError:
>      print(f"File {FileName} not found")
>      sys.exit()
> else: # or "finally:"
>          for ln in f:
>              print("I do a lot of processing here")
>              # Many lines of code here .....

I find that much less readable because the file handling
block is a long way from the open file line and has extra
control statements to mentally negotiate.

The advantage of the original version is that you can
ignore errors and read the code easily. You only need
to find the except clause if you need to know how errors
will be dealt with. But if comprehending the core
functionality of the code you can just ignore all
the error handling blocks.

> Is there a better / more Pythonic solution?

All IMHO of course, but I think the current implementation
is the best of the options presented.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Best use of "open" context manager Alan Gauld <learn2program@gmail.com> - 2024-07-06 12:43 +0100

csiph-web