Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Working with paths

15 views
Skip to first unread message

Peter Slížik

unread,
Jul 16, 2023, 9:58:51 AM7/16/23
to
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?

Peter

Kushal Kumaran

unread,
Jul 16, 2023, 1:28:04 PM7/16/23
to
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
0 new messages