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

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

Path: ...!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: Left Right <olegsivokon@gmail.com>
Newsgroups: comp.lang.python
Subject: Re: FileNotFoundError thrown due to file name in file, rather than
 file itself
Date: Mon, 11 Nov 2024 17:04:52 +0100
Lines: 80
Message-ID: <mailman.92.1731341107.4695.python-list@python.org>
References: <87v7wt986z.fsf@zedat.fu-berlin.de>
 <CAJQBtg=UOiOmmHa25EUZtrZO19F1O0_VxCO6gWjZ5ebAMHnXCA@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de 7Q4FlMQx4wbszB5QStolGwDBAnR1BSEWRjouw2pOoaGg==
Cancel-Lock: sha1:hpDTbC/TzYBM8Qabkh4pS4T4txQ= sha256:fgdM3dImhvZEbb3VmhmFTsHJlNZNITepqSmLRgJiLKs=
Return-Path: <olegsivokon@gmail.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=gmail.com header.i=@gmail.com header.b=XnBcuM1x;
 dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.005
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'salt': 0.04; 'error:':
 0.05; 'programming.': 0.05; 'subject:name': 0.07; 'url:mailman':
 0.09; 'cases.': 0.09; 'cc:addr:python-list': 0.09; 'numeric':
 0.09; 'reporting': 0.09; 'skip:[ 20': 0.09; 'cheers,': 0.11;
 'log': 0.12; 'cc:no real name:2**0': 0.14; 'that.': 0.15;
 'url:listinfo': 0.15; 'bennett': 0.16; 'customized': 0.16;
 'found.': 0.16; 'generating': 0.16; 'infra': 0.16; 'level,': 0.16;
 'logs': 0.16; 'namely': 0.16; 'program:': 0.16; 'received:mail-
 qv1-xf30.google.com': 0.16; 'wrote:': 0.16; 'problem': 0.16;
 'python': 0.16; 'code.': 0.19; 'cc:addr:python.org': 0.20; 'url-
 ip:188.166.95.178/32': 0.20; 'url-ip:188.166.95/24': 0.20;
 'language': 0.21; 'exception': 0.22; 'ran': 0.22; 'returns': 0.22;
 'subject:file': 0.22; 'version': 0.23; 'code': 0.23; 'url-
 ip:188.166/16': 0.24; "i'd": 0.24; 'idea': 0.24; '(and': 0.25;
 'anything': 0.25; 'cc:2**0': 0.25; 'tried': 0.26; '11,': 0.26;
 'skip:{ 20': 0.26; "wasn't": 0.26; 'creating': 0.27; "isn't":
 0.27; 'else': 0.27; 'error': 0.29; 'message-id:@mail.gmail.com':
 0.31; 'program': 0.32; 'question': 0.32; 'python-list': 0.32;
 'suitable': 0.32; 'but': 0.32; 'unless': 0.32; 'there': 0.33;
 'path': 0.33; 'header:In-Reply-To:1': 0.34; 'received:google.com':
 0.34; 'printing': 0.34; 'from:addr:gmail.com': 0.34; 'following':
 0.35; 'mon,': 0.36; 'source': 0.36; 'those': 0.36; 'missing':
 0.37; 'special': 0.37; 'currently': 0.37; "it's": 0.37; 'could':
 0.37; 'though': 0.37; 'file': 0.38; 'use': 0.39; 'two': 0.39;
 "that's": 0.39; 'still': 0.40; 'case.': 0.40; 'file:': 0.40;
 'files.': 0.40; 'should': 0.40; 'tell': 0.60; "there's": 0.61;
 'true': 0.63; 'simply': 0.63; 'between': 0.63; 'your': 0.64;
 'universal': 0.64; 'bad': 0.67; 'management': 0.68; 'terms': 0.69;
 'little': 0.73; 'tools': 0.74; 'skip:f 20': 0.75; 'accordingly.':
 0.76; 'choice': 0.76; 'heart': 0.76; 'poor': 0.76; 'potentially':
 0.76; 'signature': 0.76; 'discovered': 0.80; 'attribute': 0.84;
 'figuring': 0.84; 'subject: \n ': 0.84; 'surprised': 0.84;
 'caused': 0.86; 'incorporates': 0.91; 'subject:than': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1731341104; x=1731945904; darn=python.org;
 h=content-transfer-encoding:cc:to:subject:message-id:date:from
 :in-reply-to:references:mime-version:from:to:cc:subject:date
 :message-id:reply-to;
 bh=psCAT2Lz8xNOvuq7G5JjumRdh7fJ01Wzi95W5VMDFYA=;
 b=XnBcuM1x1eh4H8vZGD5n4PIcV3+bsEnB0KYlage2JLKh0NxMr7s115bueWu7FIccyV
 F0BITxP//xC+Cu1RqwxaS6Q4lCIT9pdoFGsiAbuSk3GpSZf7eyNxueUs60ePuWNIBaGg
 m90fCpXul4wdPVuciFvtDIt59q7LREtK9mhb1q6h3c5/Cv9vx47N9q5molwWmvZ6Ab7M
 xyn5I8XpmVFaSEHecOxkHv9XhCnHClRa3yJ36DFhJ3agrLN0jyeifgWamye/8JiexRTV
 LtWYwx1dAYMSyqlHtNeMAESL0ACLsl+seLza2TDe3q2aY4Ud7pYVj6/nV5HlynYejQLW
 UImA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1731341104; x=1731945904;
 h=content-transfer-encoding:cc:to:subject:message-id:date:from
 :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=psCAT2Lz8xNOvuq7G5JjumRdh7fJ01Wzi95W5VMDFYA=;
 b=bOcnrQgmaYwr54m4F1KZx/iTdWuZdCocEImGkHtC9dvsz0+Ihia1AUDtBREDc4ZXRG
 OBdQIioXl55xrbJsaG9LTh4RZ9XwIbyL8oX7fVVUTuldnn13wLbjDtnkOg/EylvzJyV/
 Vfjki/dr//43iwbELTSzDT85bt4wGkAiK7z7vj8TvQBHti2P64oRGUaeO0RkIxaelU5Q
 LPoTu7xPWTOIKZG5U18GYH/K4E9sNIhDO6jzXgcdt/Rcm7EYdePZKSGQbdZdzyVdvbzc
 jKYsyU2ywQPtr1uV9qicboQiEkABhZS8UKXWkWMppMsEMHMD6WmU8l2Cv556QjOYwg1/
 Y8aw==
X-Gm-Message-State: AOJu0Yyr7to13icAcQjR2DjGLga6ghDgTX80n0P73DEhJHy38pEDAOMx
 dA56GpVEwVW5iN5YEOpDivWPwoCCx+WVZE5nVe6hEW42ffx6CJEm3neB5cyG04wLn0nPdo+N2cH
 uEJ8N5rfLXX9ppRQLSe8v+EfueLkR1S88
X-Google-Smtp-Source: AGHT+IEhxayK29aZiS6o+MlHoqGZ4BZPATDHF+IbTYo6dENI5esMsz7hBlqn8qWCA4UzIs2NrfOkcIm15+KBakxpfIM=
X-Received: by 2002:a05:6214:398d:b0:6cb:e860:eae6 with SMTP id
 6a1803df08f44-6d39e1078abmr160209216d6.5.1731341103908; Mon, 11 Nov 2024
 08:05:03 -0800 (PST)
In-Reply-To: <87v7wt986z.fsf@zedat.fu-berlin.de>
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: <CAJQBtg=UOiOmmHa25EUZtrZO19F1O0_VxCO6gWjZ5ebAMHnXCA@mail.gmail.com>
X-Mailman-Original-References: <87v7wt986z.fsf@zedat.fu-berlin.de>
Bytes: 9306

Poor error reporting is a very common problem in programming.  Python
is not anything special in this case.  Of course, it would've been
better if the error reported what file wasn't found.  But, usually
these problems are stacking, like in your code.  Unfortunately, it's
your duty, as the language user, to anticipate those problems and act
accordingly. Now you've learned that the one file you believe that
could be the source for the error isn't the only one--well, adjust
your code to differentiate between those two (and potentially other?)
cases.  There's very little else you can do beside that.

NB. On the system level, the error has no information about what file
wasn't found.  It simply returns some numeric value (the famous
ENOENT) in case when the system call to open a file fails.  Python
could've been more helpful by figuring out what path caused the
problem and printing that in the error message, but it doesn't...
That's why I, myself, never use the vanilla FileNotFoundError, I
always re-rise it with a customized version that incorporates the
information about the missing file in the error message.

NB2. It's always a bad idea to print logs to files.  Any sysadmin /
ops / infra person worth their salt will tell you that.  The only
place the logs should go to is the standard error.  There are true and
tried tools that can pick up logs from that point on, and do with them
whatever your heart desires.  That is, of course, unless you are
creating system tools for universal log management (in which case, I'd
question the choice of Python as a suitable language for such a task).
Unfortunately, even though this has been common knowledge for decades,
it's still elusive in the world of application development :|

On Mon, Nov 11, 2024 at 4:00=E2=80=AFPM Loris Bennett via Python-list
<python-list@python.org> wrote:
>
> Hi,
>
> I have the following in my program:
>
>     try:
>         logging.config.fileConfig(args.config_file)
>         config =3D 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.")
>         sys.exit(0)
>
> and when I ran the program I got the error
>
>   Error: configuration file /usr/local/etc/sc_mailer not found.  Exiting.
>
> However, this file *does* exist and *can* be read.  By checking the
> 'filename' attribute of the exception I discovered that the problem was
> the log file defined *in* the config file, namely
>
>   [handler_fileHandler]
>   class=3DFileHandler
>   level=3DDEBUG
>   formatter=3DdefaultFormatter
>   args=3D('/var/log/my_prog.log', 'a')
>
> This log file did not exist.  The exception is thrown by
>
>   logging.config.fileConfig(args.config_file)
>
> My questions are:
>
> 1. Should I be surprised by this behaviour?
> 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?
>
> Cheers,
>
> Loris
>
> --
> This signature is currently under constuction.
> --
> https://mail.python.org/mailman/listinfo/python-list