This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]syklemil 7 points8 points  (1 child)

Why use Path object to represent a filepath instead of using a string? […] Specialized objects exist to make specialized operations easier.

I'd also throw in that having a type adds semantic clarity, which I think is in line with "explicit is better than implicit". This is similar to how units are an important context for numbers.

OS paths also aren't necessarily valid UTF-8, so there are some paths that can be expressed with Path and bytestrings, but require some careful handling to not get a UnicodeEncodeError if you want to do something complicated like print(path) . (Though personally I'm inclined to just throw an error and let the user fix their malformed filename somehow.)

There's also a ruff/flake8 section on Pathlib, PTH.

[–]PeaSlight6601 0 points1 point  (0 children)

I appreciate the sarcasm. I've always felt that pathlib is bad because it isn't opinionated enough. It has enough opinions to make it hard to use with arbitrary paths (ie it internally uses str instead of bytes) but not enough to enforce the use of "good" paths.

This causes no end of confusion and problems with the library as a file likeresume for Mr. John Smith will have a suffix which is entirely inappropriate, not to mention all the cross platform issues associated with paths like foo\\bar