34

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 ?

Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57
user7579349
  • 581
  • 1
  • 5
  • 10
  • 2
    `PurePath` is the set of functions that can figure out things just from the paths you give it. it doesn't need to look up anything. `Path` is the set of functions that need to do actual look ups on the filesystem. `Path` can inherit the `PurePath` functions because they will still work even that way. – Skaperen Aug 05 '19 at 03:59
  • 1
    `.parent` does not need to do any lookups because all it needs to do is take of the last name separated by / or \. thus it qualifies to be in `PurePath`. – Skaperen Aug 05 '19 at 04:02
  • if it's in `PurePath` you can choose to use `Path` instead. – Skaperen Aug 05 '19 at 04:06
  • @user7579349 If my answer helped you, please consider marking answer as accepted. – Dinko Pehar Nov 25 '21 at 18:40

1 Answers1

42

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.

Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57
  • 2
    As often, the examples (of I/O operations) helped to bring the point across to me, thanks – gebbissimo Jul 16 '21 at 09:39
  • Whether I need I/O operations or not, why wouldn't I just use `Path`, if for no other reason than one less option to remember? Is there some disadvantage to using `Path` only for the string manipulation features? Maybe it's safer to use `PurePath` in that case to prevent accidental use of I/O operations? – acetone Apr 27 '22 at 05:37
  • @acetone You're right ! It's provided right under the diagram: Pure paths are useful in some special cases; for example, 1. If you want to manipulate Windows paths on a Unix machine (or vice versa). You cannot instantiate a WindowsPath when running on Unix, but you can instantiate PureWindowsPath. 2. You want to make sure that your code only manipulates paths without actually accessing the OS. In this case, instantiating one of the pure classes may be useful since those simply don’t have any OS-accessing operations. – Dinko Pehar May 03 '22 at 08:52