Deutsch English Français Italiano |
<mailman.3.1720263159.2981.python-list@python.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.mixmin.net!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Rob Cliffe <rob.cliffe@btinternet.com> Newsgroups: comp.lang.python Subject: Best use of "open" context manager Date: Sat, 6 Jul 2024 11:49:06 +0100 Lines: 61 Message-ID: <mailman.3.1720263159.2981.python-list@python.org> References: <954c4ca8-cf37-4482-a1be-46d39cb503f9@btinternet.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de gQdxhI1pAIs1TQsn0wRkHw3QqkDK0T8jycAkF8Sk624g== Cancel-Lock: sha1:GtwKgiJ4ECETiawHu8r3uhbW9Ao= sha256:eUG9M80DVoU6KgPqoJ6Zt6sGpakngDxxyycQ8Cs98h8= Return-Path: <rob.cliffe@btinternet.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=btinternet.com header.i=@btinternet.com header.b=Yd06McLN; dkim-adsp=pass; dkim-atps=neutral X-Spam-Status: OK 0.079 X-Spam-Evidence: '*H*': 0.85; '*S*': 0.00; 'this:': 0.03; '(which': 0.04; 'e.g.': 0.07; 'action,': 0.09; 'else:': 0.09; 'memory.': 0.09; 'valueerror:': 0.09; 'decreases': 0.16; 'pythonic': 0.16; 'targeted': 0.16; 'to:addr:python-list': 0.20; 'closed': 0.22; 'ran': 0.22; 'code': 0.23; 'lines': 0.23; 'seems': 0.26; 'manager,': 0.26; 'error': 0.29; 'header:User-Agent:1': 0.30; 'attempt': 0.31; 'modify': 0.31; 'context': 0.32; 'to:name:python': 0.32; 'received:192.168.1': 0.32; 'but': 0.32; 'there': 0.33; 'work,': 0.36; 'really': 0.37; 'using': 0.37; 'received:192.168': 0.37; 'file': 0.38; 'could': 0.38; 'put': 0.38; 'read': 0.38; 'text': 0.39; 'program.': 0.40; 'wishes': 0.40; 'something': 0.40; 'want': 0.40; 'should': 0.40; 'best': 0.61; 'kept': 0.61; 'skip:o 10': 0.61; 'here': 0.62; 'small,': 0.69; 'suite': 0.71; 'exceptions': 0.84; 'forgot': 0.84; 'readability.': 0.84; 'rob': 0.84; 'subject:manager': 0.84; 'subject:open': 0.84; 'violates': 0.84; 'loses': 0.91; 'race': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=btinternet.com; s=btmx2012311; t=1720263158; bh=+mGosCyMsnajjC1Ha7qejdTZIpqrKg/+1TunNUBJRTI=; h=Message-ID:Date:MIME-Version:To:From:Subject; b=Yd06McLN9bzfKpebjxhSyIIzYLoWo64IMvPAaw7pLfgZ7iW1NPfHqyH1cQY0qIziad+xyTgJSKF1iitZcDjoY5s3uC0153AnuPzm8DHAB0bsMUsHiINObQRM3z3nRtgfhzx82J0GwEzQxIx+e4XsYY8kUvMDxCiJmUuvvI4Ee4WliZVuX1E6vtXExSZuSUFZaye2J0B0TVM47RxTIzP5Lf8NFtFcdR2FpUm0GgExaVassstQULBkOu0PUHZ9OO2xpb7WuIIreKPTKW1ikb1OvJ/2jx94Zux7daEEqbjDnwLHw0nUmAUYzq7KNin45J29DC/odr3Xl6LC58HoFcvMSg== X-SNCR-Rigid: 666A0E5601835497 X-Originating-IP: [86.139.197.7] X-OWM-Source-IP: 86.139.197.7 X-OWM-Env-Sender: rob.cliffe@btinternet.com X-VadeSecure-score: verdict=clean score=0/300, class=clean X-RazorGate-Vade: gggruggvucftvghtrhhoucdtuddrgeeftddrvdefgdeffecutefuodetggdotefrodftvfcurfhrohhfihhlvgemuceutffkvffkuffjvffgnffgvefqofdpqfgfvfenuceurghilhhouhhtmecufedtudenucenucfjughrpefkffggfgfvhffutgfgsehtkeertddtvdejnecuhfhrohhmpeftohgsucevlhhifhhfvgcuoehrohgsrdgtlhhifhhfvgessghtihhnthgvrhhnvghtrdgtohhmqeenucggtffrrghtthgvrhhnpeejieegffeivefhjeduhefhjeetgfeuffdvgfeggfdvteeftedufeevuefghefhteenucfkphepkeeirddufeelrdduleejrdejnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehhvghloheplgduledvrdduieekrddurddvtdekngdpihhnvghtpeekiedrudefledrudeljedrjedpmhgrihhlfhhrohhmpehrohgsrdgtlhhifhhfvgessghtihhnthgvrhhnvghtrdgtohhmpdhnsggprhgtphhtthhopedupdhrtghpthhtohepphihthhhohhnqdhlihhsthesphihthhhohhnrdhorhhgpdhrvghvkffrpehhohhsthekiedqudefledqudeljedqjedrrhgrnhhgvgekiedqudefledrsghttggvnhhtrhgrlhhplhhushdrtghomhdprghuthhhpghushgvrheprhhosgdrtghlihhffhgvsegsthhinhhtvghrnhgvthdrtghomhdpghgvohfkrfepifeupdfovfetjfhoshhtpegsthhprhgurhhgohdttdeg X-RazorGate-Vade-Verdict: clean 0 X-RazorGate-Vade-Classification: clean X-SNCR-hdrdom: btinternet.com X-SNCR-hdrdom: btinternet.com X-SNCR-hdrdom: btinternet.com User-Agent: Mozilla Thunderbird Content-Language: en-GB 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: <954c4ca8-cf37-4482-a1be-46d39cb503f9@btinternet.com> Bytes: 6757 Consider this scenario (which I ran into in real life): I want to open a text file and do a lot of processing on the lines of that file. 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, so that only targeted exceptions are trapped, not to mention that having "try" and "except" far apart decreases readability. Or I might write it like this: 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 ..... but this loses the benefits of using "open" as a context manager, and would also be unacceptable if the file was too large to read into memory. 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 ..... but this of course does not work because by the time we get to "for ln in f:" the file has been closed so we get ValueError: I/O operation on closed file I could modify the last attempt to open the file twice, which would work, but seems like a kludge (subject to race condition, inefficient). Is there a better / more Pythonic solution? Best wishes Rob Cliffe