Deutsch   English   Français   Italiano  
<mailman.95.1731373814.4695.python-list@python.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.nobody.at!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: dn <PythonList@DancesWithMice.info>
Newsgroups: comp.lang.python
Subject: Re: FileNotFoundError thrown due to file name in file, rather than
 file itself
Date: Tue, 12 Nov 2024 14:10:01 +1300
Organization: DWM
Lines: 66
Message-ID: <mailman.95.1731373814.4695.python-list@python.org>
References: <26418.15836.335097.984240@ixdm.fritz.box>
 <ZzJ0eoWZds3xSKWn@cskk.homeip.net>
 <635ef074-15b3-4084-ad34-c350b150a794@DancesWithMice.info>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 74gRSwWDm4OkHKjklmD5XASZmjN8yvkwgHjZsiNpdNzg==
Cancel-Lock: sha1:C9MOcUJSUXNzfDdRxRcNyLfQTO8= sha256:RyfZ8VNgDV+JEauYXmlJ+zccCEcmXrBMm0z8McrI2dA=
Return-Path: <PythonList@DancesWithMice.info>
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=danceswithmice.info header.i=@danceswithmice.info
 header.b=Lw91ny41; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.024
X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; '(which': 0.04;
 'subject:name': 0.07; '=dn': 0.09;
 'from:addr:danceswithmice.info': 0.09; 'from:addr:pythonlist':
 0.09; 'skip:\xc2 20': 0.09; 'log': 0.12; '*nix': 0.16; '10:17,':
 0.16; 'bennett': 0.16; 'cameron': 0.16; 'found:': 0.16;
 'generating': 0.16; 'indentation': 0.16; 'message-
 id:@DancesWithMice.info': 0.16; 'program:': 0.16;
 'received:cloud': 0.16; 'received:rangi.cloud': 0.16; 'rough':
 0.16; 'simpson': 0.16; 'wrote:': 0.16; 'problem': 0.16; 'python':
 0.16; 'code.': 0.19; 'to:addr:python-list': 0.20; 'subject:file':
 0.22; 'url:wiki': 0.23; 'code': 0.23; 'configure': 0.26; 'skip:{
 20': 0.26; '>>>': 0.28; 'error': 0.29; 'header:User-Agent:1':
 0.30; 'execution': 0.32; 'python-list': 0.32;
 'header:Organization:1': 0.32; 'but': 0.32; 'header:In-Reply-
 To:1': 0.34; 'following': 0.35; 'others': 0.37;
 'received:192.168': 0.37; 'file': 0.38; 'way': 0.38; 'put': 0.38;
 'read': 0.38; 'handle': 0.39; 'block': 0.39; 'wrote': 0.39;
 '(see': 0.40; 'explain': 0.40; 'file:': 0.40; 'should': 0.40;
 'english': 0.60; 'skip:\xc2 10': 0.62; 'subject': 0.63; 'between':
 0.63; 'research': 0.64; 'definition': 0.64; 'generally': 0.67;
 'terms': 0.69; 'and,': 0.69; 'piece': 0.69; 'url:wikipedia': 0.70;
 '8bit%:100': 0.76; 'advise': 0.78; 'so:': 0.84; 'subject: \n ':
 0.84; 'surprised': 0.84; 'thus,': 0.84;
 '\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84; '"do':
 0.91; 'subject:than': 0.91
DKIM-Filter: OpenDKIM Filter v2.11.0 vps.rangi.cloud 07B64805B
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info;
 s=staff; t=1731373806;
 bh=lNLHizo/VOR+dKQcUF/SKW7mlwgYET/EM83G9GBMF/s=;
 h=Date:From:Subject:To:References:In-Reply-To:From;
 b=Lw91ny41onkP1zT9zVOtznt432KpZqSIqQSPU5JF66kV/Aao6gJ6T0a+dFIjAzIdw
 2F/aEoTuSpMQt/atjBh0uLyPzBZXQi6TXx2yjsKb5E6Y9E8sKCuPGUJnCv7lNLwgKq
 D1iaQ0VslyDJIgsbyfYIcm8aFXcrMlzqgNzlgB3myDW9y6lNzRLJ7dcPW09KYVhUM4
 bS/v9TSC4MHQgC9U/OiF7Dk0/7CW1wcqQHB/IFFvjSzYpw1ATSufu4LiAvN3UTp7Jz
 jSOuMxGekqpX1L60ciA9bBZGC25/Y6PIcv3+0W8gE+JOrVuTyXuVdTw9G+0wXqK0gN
 fQnEF8fDdAXHA==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <ZzJ0eoWZds3xSKWn@cskk.homeip.net>
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: <635ef074-15b3-4084-ad34-c350b150a794@DancesWithMice.info>
X-Mailman-Original-References: <26418.15836.335097.984240@ixdm.fritz.box>
 <ZzJ0eoWZds3xSKWn@cskk.homeip.net>
Bytes: 6574

On 12/11/24 10:17, Cameron Simpson via Python-list wrote:
> 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.")
>>
> My questions are:
> 
> 1. Should I be surprised by this behaviour?

No. Python has behaved as-programmed.


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

Augmenting @Cameron's excellent advice: please research "Separation of 
Concerns", eg https://en.wikipedia.org/wiki/Separation_of_concerns 
(which overlaps with one interpretation of SOLID's "Single 
Responsibility Principle" and the *nix philosophy of "do one thing, and 
do it well").

If you were to explain the code-snippet in English (or ...) it has 
several parts/concerns:

- configure the log
- instantiate ConfigParser()
- read the env.file
- advise verbosity-setting
- handle file-error
- (and, but not appearing) terminate execution

A block of code (see indentation for rough definition of "block") should 
achieve one thing ("concern"). Thus, the advice to separate-out the 
file-read and attendant defensive-coding.

This anticipates the problem (2) of distinguishing the subject of any 
one error/stack-trace from any others - and, arguably, makes the code 
easier to read.


-- 
Regards,
=dn