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


Groups > comp.lang.python > #197006

Re: FileNotFoundError thrown due to file name in file, rather than file itself

From Karsten Hilbert <Karsten.Hilbert@gmx.net>
Newsgroups comp.lang.python
Subject Re: FileNotFoundError thrown due to file name in file, rather than file itself
Date 2024-11-12 18:47 +0100
Message-ID <mailman.99.1731433661.4695.python-list@python.org> (permalink)
References <26418.15836.335097.984240@ixdm.fritz.box> <ZzJ0eoWZds3xSKWn@cskk.homeip.net> <mailman.94.1731359879.4695.python-list@python.org> <87o72kygts.fsf@zedat.fu-berlin.de> <ZzOUuVy6s0Ao3ZKq@hermes.hilbert.loc>

Show all headers | View raw


Am Tue, Nov 12, 2024 at 09:52:31AM +0100 schrieb Loris Bennett via Python-list:

> 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.

Well:

	>>> help(config.read)
	Help on method read in module configparser:

	read(filenames, encoding=None) method of configparser.ConfigParser instance
	    Read and parse a filename or an iterable of filenames.

	    Files that cannot be opened are silently ignored; this is
	    designed so that you can specify an iterable of potential
	    configuration file locations (e.g. current directory, user's
	    home directory, systemwide directory), and all existing
	    configuration files in the iterable will be read.  A single
	    filename may also be given.

	    Return list of successfully read files.

So, the very fact that it does not return any output AND
returns an empty list is the (intended) way of knowing the
error state.

Karsten
--
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B

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


Thread

Re: FileNotFoundError thrown due to file name in file, rather than file itself Cameron Simpson <cs@cskk.id.au> - 2024-11-12 08:17 +1100
  Re: FileNotFoundError thrown due to file name in file, rather than file itself "Loris Bennett" <loris.bennett@fu-berlin.de> - 2024-11-12 09:52 +0100
    Re: FileNotFoundError thrown due to file name in file, rather than file itself Karsten Hilbert <Karsten.Hilbert@gmx.net> - 2024-11-12 18:47 +0100
    Re: FileNotFoundError thrown due to file name in file, rather than file itself Rob Cliffe <rob.cliffe@btinternet.com> - 2024-11-12 21:04 +0000

csiph-web