Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Exploring the Finer Details of Path

00:00 The Finer Details of Path. In this section of the course, you’ll take a look at some of the details of paths, which will help you understand them in more depth, starting out at looking at differences between operating systems.

00:13 Earlier in the course, you saw that when we instantiated a pathlib.Path, either a WindowsPath or PosixPath object was returned, depending on the operating system you’re using.

00:23 This feature makes it fairly easy to write cross-platform compatible code. It is possible to ask for a WindowsPath or a PosixPath explicitly, but you will only be limiting your code to that system without any benefits.

00:39 A concrete path like this cannot be used on a different system.

00:53 There might be times when you need a representation of a path without access to the underlying file system, in which case it could also make sense to represent a Windows path on a non-Windows system or vice versa. This can be done with PurePath objects.

01:07 These objects support the operations to pick out components of a path, but not the methods that access the file system.

01:39 You can directly instantiate PureWindowsPath or PurePosixPath on all systems. Instantiating PurePath will return one of these objects depending on the operating system you’re using. Here, PurePath is run on macOS … and here, on Windows.

02:01 Note that the same code generates a different PurePath on each operating system.

02:12 At the start of the course, you saw that paths are not strings, and one motivation behind pathlib is to represent the file system with proper objects. In fact, the documentation of pathlib is titled pathlib — Object-oriented filesystem paths.

02:27 The object-oriented approach is already visible in the examples you’ve seen, but here you will see some other examples illustrating this. Independently of the operating system you’re using, paths are represented in Posix style, with the forward slash as the path separator. On Windows, you’ll see something similar to what’s seen on-screen.

02:55 When a path is converted to a string, it will use the native form, so you’ll see backslashes on Windows.

03:07 This is particularly useful if you’re using a library that doesn’t know how to deal with pathlib.Path objects. This is a bigger problem on Python versions before 3.6, but it’s something you may experience when working with older unmaintained libraries, which can happen when working with large, mature projects.

03:27 As an example, in Python 3.5, the configparser standard library can only use string paths to read files. The way to handle such cases is to do the conversion to a string explicitly.

04:08 It’s considered best practice to use os.fspath() instead of str() if you need to do an explicit conversion. This is a little safer, as it will raise an error if you accidentally try to convert an object that is not pathlike, as seen on-screen.

04:27 Potentially the most controversial part of the pathlib library is the use of the / operator. For a little peek under the hood, let’s see how that’s implemented. This is an example of operator overloading: the behavior of an operator is changed depending on the context.

04:43 You have seen this before. Think about how + means different things for strings and numbers. Python implements operator overloading through the use of double underscore methods, a.k.a. dunder methods.

04:57 The / operator is defined by the .__truediv__() method. In fact, if you take a look at the source code of pathlib, you’ll see the code seen on-screen, which implements the behavior you’ve already used.

05:14 In the next section of the course, you’ll see some examples of pathlib in action.

Become a Member to join the conversation.