Re: Working with paths
On Sun, Jul 16 2023 at 03:58:07 PM, Peter Slížik wrote: > Hello, > > I finally had a look at the pathlib module. (Should have done it long ago, > but anyway...). Having in mind the replies from my older thread (File > system path annotations), what is the best way to support all possible path > types? > > def doit(path: str | bytes | os.PathLike): > match path: > case str() as path: > print("string") > > case bytes() as path: > print("bytes") > > case os.PathLike() as path: > print("os.PathLike") > > Should I branch on the individual types or is there a more elegant way? > Depends on what you need to do with the path. The best way, imo, is to simply use pathlib.Path and ignore the existence of other path representations. If input is coming from elsewhere, convert it to pathlib.Path as early as possible. In the main body of your code, it should be able to rely on all paths being Path objects. -- regards, kushal -- https://mail.python.org/mailman/listinfo/python-list
Re: Too Broad of an exception
On Wed, Oct 25 2023 at 11:49:12 AM, rsutton wrote: > On 10/25/2023 11:06 AM, Stefan Ram wrote: >> r...@zedat.fu-berlin.de (Stefan Ram) writes: >>> outer quotation marks) prints some prominent exception types. After >>> manually removing those that do not seem to apply, I am left with: >>> "AssertionError", >>> "ChildProcessError", >> ... >>"Manually removing" above was meant to be a fast first pass, >>where I only excluded exception types that were obviously >>inappropriate. It is now to be followed by a search for the >>appropriate exception types among those exception types left. >> > @Rene & @Stefan, > I really appreciate the guidance provided. By replacing Exception > with RuntimeError, pylint seems happy! More specificity, I guess. I > know that I could have ignored the pylint exceptions, but I want to > use this as a learning experience. I looks like I have a lot of > reading to do on exception handling. IMO all of the try/except code > looks quite clumsy to me. It may be excellent for some tasks but to > me, it looks quite inelegant. Like I said, I have a lot to learn. > >From what you've described of your problem, it seems like a small-ish utility program you're writing for your own use. You don't need any `try`...`except` blocks in such code. You just let the exception stop your program. -- regards, kushal -- https://mail.python.org/mailman/listinfo/python-list
Re: FileNotFoundError thrown due to file name in file, rather than file itself
On Wed, Nov 13 2024 at 07:36:04 PM, dieter.mau...@online.de wrote: > Loris Bennett wrote at 2024-11-12 10:00 +0100: >> ... >>However, it strikes me as not immediately obvious that the logging file >>must exist at this point. I can imagine a situation in which I want to >>configure a default log file and create it if it missing. > > This is what happens usually: > if you open a file with mode `a` or `w`, the file is created > if it does not yet exist. > > Thus, a missing log file should not give you the `FileNotFound` > exception. > Look at the exception details: they should tell you what really > was not found (maybe the directory for the logfile). It is possible a directory along the path does not exist. -- regards, kushal -- https://mail.python.org/mailman/listinfo/python-list