Deutsch English Français Italiano |
<mailman.10.1720315042.2981.python-list@python.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail From: Cameron Simpson <cs@cskk.id.au> Newsgroups: comp.lang.python Subject: Re: Best use of "open" context manager Date: Sun, 7 Jul 2024 11:08:33 +1000 Lines: 24 Message-ID: <mailman.10.1720315042.2981.python-list@python.org> References: <954c4ca8-cf37-4482-a1be-46d39cb503f9@btinternet.com> <ZonqkTYNCiNQRQSv@cskk.homeip.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de TgMZMq8V9MkVSlYve9ftUA4dZrArmARXP0WrYOgfUkJQ== Cancel-Lock: sha1:X7YktMXbn/I4ZyA+2ZW4VaQPwyM= sha256:YUnOnxrfZt9Bejsc1g3TstMzasYQKZUd1RTZ7zfkBbE= Return-Path: <cameron@cskk.id.au> X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org Authentication-Results: mail.python.org; dkim=none reason="no signature"; dkim-adsp=none (unprotected policy); dkim-atps=neutral X-Spam-Status: OK 0.017 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'this:': 0.03; 'cc:addr :python-list': 0.09; 'cc:name:python': 0.16; 'from:addr:cs': 0.16; 'from:addr:cskk.id.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'received:13.237': 0.16; 'received:13.237.201': 0.16; 'received:13.237.201.189': 0.16; 'received:cskk.id.au': 0.16; 'received:id.au': 0.16; 'received:mail.cskk.id.au': 0.16; 'remember,': 0.16; 'wrote:': 0.16; 'cc:addr:python.org': 0.20; 'returns': 0.22; 'code': 0.23; 'lines': 0.23; 'cc:2**0': 0.25; 'object': 0.26; 'header:User- Agent:1': 0.30; 'context': 0.32; 'header:In-Reply-To:1': 0.34; 'received:au': 0.35; 'file': 0.38; 'put': 0.38; 'skip:o 10': 0.61; 'here': 0.62; 'received:13': 0.64; 'received:userid': 0.66; 'forgot': 0.84; 'itself.': 0.84; 'rob': 0.84; 'subject:manager': 0.84; 'subject:open': 0.84 Mail-Followup-To: Rob Cliffe <rob.cliffe@btinternet.com>, Python <python-list@python.org> Content-Disposition: inline In-Reply-To: <954c4ca8-cf37-4482-a1be-46d39cb503f9@btinternet.com> User-Agent: Mutt/2.2.13 (2024-03-09) 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: <ZonqkTYNCiNQRQSv@cskk.homeip.net> X-Mailman-Original-References: <954c4ca8-cf37-4482-a1be-46d39cb503f9@btinternet.com> Bytes: 3690 On 06Jul2024 11:49, Rob Cliffe <rob.cliffe@btinternet.com> wrote: >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 -:) >for ln in File Lines: > print("I do a lot of processing here") > # Many lines of code here ..... What about this: try: f = open(FileName) as f: except FileNotFoundError: print(f"File {FileName} not found") sys.exit() with f: ... process the lines here ... Remember, the `open()` call returns a file object _which can be used as a context manager_. It is separate from the `with` itself.