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

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

Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail
From: Cameron Simpson <cs@cskk.id.au>
Newsgroups: comp.lang.python
Subject: Re: FileNotFoundError thrown due to file name in file, rather than
 file itself
Date: Tue, 12 Nov 2024 08:17:46 +1100
Lines: 41
Message-ID: <mailman.94.1731359879.4695.python-list@python.org>
References: <26418.15836.335097.984240@ixdm.fritz.box>
 <ZzJ0eoWZds3xSKWn@cskk.homeip.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
X-Trace: news.uni-berlin.de MO7U7ow8q/qHv68/pzlk9g2x+SkTUAGdMl7ffe9rp16g==
Cancel-Lock: sha1:j3ajq3R70xUsA6vE3arMIXqyjvA= sha256:JoiI5fYMNa0SjXdbMDktPxYEKKsDk/RuYIB/dZrmBTU=
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.009
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:name': 0.07;
 '(python': 0.09; 'cc:addr:python-list': 0.09; 'cheers,': 0.11;
 'log': 0.12; 'cc:no real name:2**0': 0.14; 'bennett': 0.16;
 'cameron': 0.16; 'found.': 0.16; 'found:': 0.16; 'from:addr:cs':
 0.16; 'from:addr:cskk.id.au': 0.16; 'from:name:cameron simpson':
 0.16; 'generating': 0.16; 'message-id:@cskk.homeip.net': 0.16;
 'program:': 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;
 'simpson': 0.16; 'wrote:': 0.16; 'code.': 0.19;
 'cc:addr:python.org': 0.20; 'exception': 0.22; 'subject:file':
 0.22; 'cc:2**1': 0.23; 'code': 0.23; 'skip:{ 20': 0.26; 'error':
 0.29; 'header:User-Agent:1': 0.30; 'header:In-Reply-To:1': 0.34;
 'fine': 0.35; 'received:au': 0.35; 'following': 0.35; '...': 0.37;
 'file': 0.38; 'way': 0.38; 'put': 0.38; 'quite': 0.39; 'wrote':
 0.39; 'file:': 0.40; 'learn': 0.40; 'should': 0.40; 'likely':
 0.61; 'between': 0.63; 'your': 0.64; 'full': 0.64; 'received:13':
 0.64; 'well': 0.65; 'received:userid': 0.66; 'generally': 0.67;
 'terms': 0.69; 'piece': 0.69; 'raised': 0.70; 'clause': 0.84;
 'so:': 0.84; 'subject: \n ': 0.84; 'subject:than': 0.91
Mail-Followup-To: dieter.maurer@online.de,
 Loris Bennett <loris.bennett@fu-berlin.de>, python-list@python.org
Content-Disposition: inline
In-Reply-To: <26418.15836.335097.984240@ixdm.fritz.box>
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: <ZzJ0eoWZds3xSKWn@cskk.homeip.net>
X-Mailman-Original-References: <26418.15836.335097.984240@ixdm.fritz.box>
Bytes: 4861

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.

Cheers,
Cameron Simpson <cs@cskk.id.au>