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.

Creating Path Objects From Strings

00:00 The main interface for working with file paths in Python is the pathlib module. It is built in, but you will have to import it in order to work with it.

00:09 You do that by typing import pathlib. And I will head over to an IDLE session to work alongside the things that I’ll be talking about here. In the IDLE REPL, I’m start off by typing import pathlib, and it gives me access to the pathlib module. Now what do you do with the pathlib module?

00:32 Primarily you’re going to work with Path objects. Those are objects that represent a file path on your operating system, and you can create them in three different ways.

00:43 You can pass just directly a string in there that represents the file path, you can use Path.home() or Path.cwd(), which stands for current working directory, or you can use the / (forward slash) operator as well as another class method called joinpath(), which is equivalent to using the / operator.

01:06 You’ll look at all three of these options in the coming lessons, and let’s start with the one that might look the most straightforward, which is creating it from a string.

01:18 I’m here on a macOS operating system. Let’s say I want to represent a path that looks like that, so it goes /Users/Martin/, which is my username on this computer, then on to the Desktop/ and finally a file called hello.txt.

01:37 This is the path I want to represent. Now, the quickest way to create a Path object for this path is to pass in this value as a string. pathlib.path and then I create a string, paste what I wrote up there in the comment, close the parentheses, and press Enter.

01:59 And you can see that this returns a Path object, just has the value of this specific path. Now you can see that it says PosixPath here instead of just saying Path.

02:09 And the reason for that is that I am on a macOS operating system. That is a Unix-based system, like also Linux systems are. This is why it says PosixPath. If you were on a Windows computer, you’d get something like WindowsPath prepended to all of the paths that you create on that system. Otherwise, it looks pretty similar. However, if you are on a Windows system, then you might run into a problem.

02:37 Windows paths use \ characters instead of / characters to represent paths, and the \ character in Python has a special meaning because it starts an escape sequence, which means that if you pass just a plain string with a \, then Python is going to not interpret the \ character as a \ character but thinks that you want to start an escape sequence.

03:01 This will break your Windows paths if you don’t account for it, and you’ll get syntax errors. There are two ways around it. You can either use a / instead, which means you can just represent a Windows path with the normal C:, but and then instead of using a \ like you normally would, you just use a / instead.

03:24 You can do that and the pathlib library knows what to do with that, and then correctly creates a Path object that points to this specific path.

03:33 Another way of doing it is to use raw string, and you can do that in Python by prefixing the string with just the r character, so you put this r in front of the string, and then you can use \ characters like you would normally with a Windows path.

03:50 Keep in mind that this is only relevant for Windows because on Unix systems like macOS or Linux, the paths anyways use this forward slash, and that’s not reserved character for Python.

Become a Member to join the conversation.