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

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Working With Paths

00:00 Working With Paths. In this part of the course, you’ll start working with pathlib’s Path objects. You’ll look at creating paths, reading and writing files, and moving and deleting files.

00:12 So let’s get started by looking at creating paths. The main thing you’ll need to know about is the pathlib.Path() class. There are a few different ways of creating a path.

00:25 First of all, there are class methods, such as .cwd() (current working directory) and .home() (your user’s home directory).

00:41 You’ll mainly be using the Path class in this course, so you can import Path from pathlib and just use Path instead of pathlib.Path, as seen in the example on-screen, which is functionally identical to what you previously saw.

01:01 For clarity, the rest of the code used in this course will use this import of Path from pathlib, and to save time, that code will already be on-screen for each example.

01:12 Towards the end of the course, some other elements of pathlib will be used, and in those cases, the original input of pathlib will be used.

01:22 A path can also explicitly be created from its string representation. On Windows, the path separator is a backslash. However, in many contexts, \ is also used as an escape character in order to represent non-printable characters. To avoid problems, use raw string literals to represent Windows paths.

01:49 These are string literals that have an r prepended to them. In raw string literals, the backslash character represents a literal backslash. If you don’t do this, in some cases you’ll end up with a string, and therefore path, which isn’t what you want and can lead to bugs in your code.

02:21 Note the difference in output between the two versions of directory, while in other cases, you’ll generate an error straight away.

02:44 A third way to construct a path is to join the parts of the path using the special operator / (forward slash). This operator is used independently of the actual path separator on the platform.

03:01 The / can join several paths or a mix of paths and strings, as seen, as long as there is at least one path object. If you don’t like this notation, you can do the same thing with the .joinpath() method.

03:22 Note that the path is represented by either a WindowsPath or a PosixPath. The actual object representing the path depends on the underlying operating system.

03:33 Path.home() will return a WindowsPath on Windows and a PosixPath on macOS or Linux. You’ll see more about this later on in the course, but next, you’ll look at reading and writing files.

Become a Member to join the conversation.