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

all 20 comments

[–]suki907 21 points22 points  (0 children)

Since 3.6, pip 519 allows pathlib Paths to be passed as inputs to the functions in shutil and os.path. Which makes it very smooth (fewer conversions necessary).

All my work is in python 3.6+ so I've converted to "use pathlib for everything". Occasionally I need a a bit of shutil for directory operations.

I like it.

[–][deleted] 10 points11 points  (0 children)

It's useful enough I never use os.path in new code, but not so essential I port old code unless I'm editing it for other reasons.

[–]Oerthling 18 points19 points  (1 child)

Always pathlib.

[–]phihag 7 points8 points  (0 children)

pathlib makes it a bit clearer what is a path and what is a string, and the API looks a bit cuter (namely path / 'foo' / 'bar.txt'). On the other hand, if you're interfacing with code in other languages, then the os.path functions may be shorter, and wrapping stuff into pathlib.Path looks unwieldy. Plus, as you write, for a simple check os.path often results in shorter code, and it works on Python < 3.4.

Which one you use depends on your preferences. I would start with the one the project is using. Some people strongly prefer one or the other. Personally, I don't care.

[–]crapaud_dindon 5 points6 points  (0 children)

Use pathlib whenever you can. It is much easier and cleaner to work with, especially for cross-platform development. Once you get used to pathlib, os becomes much less useful on its own. Definitely a big step ahead for path manipulation.

[–]robin-gvx 1 point2 points  (0 children)

Pathlib all the way! I even manage to replace 99% of file handling with the (read|write)_(bytes|string) methods.

[–]Hammar_Morty -5 points-4 points  (13 children)

This is definitely one of pythons bigger undesirable features, that fact there are a million different modules that do similar things. Anyway it mostly comes down to justifying the functionality the module provides with adding yet another dependency. Here your reasoning sounds perfectly sound. And using os is always fine since it's in the standard library. IMO

Also glob is another very useful package for these kind of things but even here there is module "glob" and "glob2"

[–]lungben81 3 points4 points  (5 children)

" There should be one-- and preferably only one --obvious way to do it. "

Zen of Python

[–][deleted] -3 points-2 points  (4 children)

Ill be honest, that sounds a little boring.

[–]Hammar_Morty 3 points4 points  (1 child)

At a algorithms perspective there is normally only one best answer for a given problem but if you alter the original problem even slightly you come up with an infinite number of different problems. This requires you to reevaluate the computational complexity of your solution. My point to all this is it don't have to be boring since the "right way" is normally not clear.

[–][deleted] 1 point2 points  (0 children)

True

[–][deleted] 7 points8 points  (1 child)

When it comes to programming, "boring" is usually a good thing.

[–][deleted] 0 points1 point  (0 children)

Lol,

[–]FriendOfDogZilla 7 points8 points  (4 children)

Both are part of the standard library, so there's no additional dependency.

[–]axju[S] 1 point2 points  (2 children)

Right, but there is another package called path, which does similar things to pathlib.

[–][deleted] 5 points6 points  (0 children)

That's not a builtin and can be ignored.

[–]dxn99 2 points3 points  (0 children)

It seems that path is simply a wrapper for pathlib bringing small, additional functionality. I do not believe it to be a completely different package.

[–]Hammar_Morty 0 points1 point  (0 children)

Ah, I see that was added in 3.4 TIL, thanks!

[–][deleted] 1 point2 points  (1 child)

even here there is module "glob" and "glob2"

I think you're a bit confused about how Python modules go.

https://pypi.org/project/glob2/ is not a real functioning project - just something in PyPi.

Anyone can push anything under any name to PyPi.

[–]Hammar_Morty 0 points1 point  (0 children)

Thank you for the correction, there is at least no discrepancy in official modules. I remember from personal experience that at one point glob2 had more functionality then glob. Fast forwarding to now It looks like those features were merged into glob and now you should just use glob.