Reputation: 691
More than an answer to the question, I am trying to learn how to make sense of the Official Python Documentation.
I understand that Path
inherits from PurePath
, but I am unable to understand when to use which and why there is PurePath
& Path
instead of one.
In the list of alternatives, most are suggesting Path
while some are suggesting Pathlib
.
I am looking at os.path.dirname()
where they are suggesting PurePath.parent
. But I am getting the same result when I run pathlib.PurePath(file).parent.name
& pathlib.Path(file).parent.name
.
So, why did they use PurePath
for some & Path
for most. Why did they not suggest Path.parent
instead of PurePath.parent
?
Upvotes: 41
Views: 18890
Reputation: 6061
First paragraph in pathlib documentation states:
Path classes are divided between pure paths, which provide purely computational operations without I/O, and concrete paths, which inherit from pure paths but also provide I/O operations.
Pure path objects provide path-handling operations which don’t actually access a filesystem.
Concrete paths are subclasses of the pure path classes. In addition to operations provided by the former(pure path), they also provide methods to do system calls on path objects.
In conclusion, PurePath
acts like string (remove parts of path, join with another path, get parents etc). To remove directory, search directory, create a file or write to file, you must use Path
object.
Upvotes: 51