Deutsch English Français Italiano |
<87o72kygts.fsf@zedat.fu-berlin.de> 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: "Loris Bennett" <loris.bennett@fu-berlin.de> Newsgroups: comp.lang.python Subject: Re: FileNotFoundError thrown due to file name in file, rather than file itself Date: Tue, 12 Nov 2024 09:52:31 +0100 Organization: FUB-IT, Freie =?utf-8?Q?Universit=C3=A4t?= Berlin Lines: 70 Message-ID: <87o72kygts.fsf@zedat.fu-berlin.de> References: <26418.15836.335097.984240@ixdm.fritz.box> <ZzJ0eoWZds3xSKWn@cskk.homeip.net> <mailman.94.1731359879.4695.python-list@python.org> Mime-Version: 1.0 Content-Type: text/plain X-Trace: news.uni-berlin.de lnHrB1YVJ3idx1xFigXW4gWV+uy6MkIGAYNKVbhIEcwOIX Cancel-Lock: sha1:Ee5tD5KKtlIZJXE4P2TyvGh6Cy0= sha1:j+dkuimdMFTWOzZxhapLQ8+WFvw= sha256:QrmfJxqK49FmJ5oUvNgdcix0tNL9UvEMFd94edt2cNU= User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) Bytes: 3330 Cameron Simpson <cs@cskk.id.au> writes: > On 11Nov2024 18:24, dieter.maurer@online.de <dieter.maurer@online.de> wrote: >>Loris Bennett wrote at 2024-11-11 15:05 +0100: >>>I have the following in my program: >>> try: >>> logging.config.fileConfig(args.config_file) >>> config = configparser.ConfigParser() >>> config.read(args.config_file) >>> if args.verbose: >>> print(f"Configuration file: {args.config_file}") >>> except FileNotFoundError: >>> print(f"Error: configuration file {args.config_file} not found. Exiting.") >> >>Do not replace full error information (including a traceback) >>with your own reduced error message. >>If you omit your "try ... except FileNotFoundError` >>(or start the `except` clause with a `raise`), you >>will learn where in the code the exception has been raised >>and likely as well what was not found (Python is quite good >>with such error details). > > Actually, file-not-found is pretty well defined - the except action > itself is fine in that regard. > > [...] >>>2. In terms of generating a helpful error message, how should one >>> distinguish between the config file not existing and the log file not >>> existing? > > Generally you should put a try/except around the smallest possible > piece of code. So: > > config = configparser.ConfigParser() > try: > config.read(args.config_file) > except FileNotFoundError as e: > print(f"Error: configuration file {args.config_file} not found: {e}") > > This way you know that the config file was missing. I appreciate the point you make about the smallest possible piece of code, although I can imagine that this could potentially create a lot of try/except clutter and I might just want a single block and then try to catch various exceptions. Regarding your example above, if 'missingfile.py' contains the following import configparser config = configparser.ConfigParser() try: config.read('/foo/bar') except FileNotFoundError as e: print(f"Error: configuration file {config_file} not found: {e}") them python3 missingfile.py does not produce an any output for me and so does not seem to be a reliable way of handling the case where the config file does not exist. Cheers, Loris -- This signature is currently under constuction.