Using the Forward Slash Operator
The third way to create a
Path object is using the
/ operator. And I can do something like typing
pathlib.Path.home() and then concatenate another string by just using this operator that is a
/ and then followed up by a string, and you can continue doing this basically forever, as many pieces of the path you want to add. And in this example, so if you concatenate
"Desktop" and then, again with the
/ operator, the string
"hello.txt", then again you get this
Path object with the value that you’ve seen before,
00:39 Let’s play around with this a bit more in the IDLE REPL.
So you just saw on a slide that you can do
pathlib.Path and start off, for example, with my
home directory, and then I concatenate
"Desktop" and then a file named
and that creates another
Path object with the same value that you’ve seen before. Now, the interesting thing about this is that it’s an operator just like the
+ (plus) in
2 + 2 in Python is an operator, but it is specific to
So you can use the
/ only if you have a
Path object at at least one side of the operator. Otherwise, it’s not defined, and it won’t work.
And this functions in a way that it is a shortcut for another method on the
Path object that is called
.joinpath(). So what it does, it calls
I need to first create a
Path object. I do this using
.home(), as you’ve seen earlier. And then instead of using the
/ character, I can also say
.joinpath() and then pass in the string here
And then I can continue, join another path,
Running this operation with
.joinpath() is the same as using the
/ operator, and you can see it produces the same output as well.
So you can think of the
/ operator as a shortcut for this
.joinpath() class method on a
Path object, and it just makes it more convenient to quickly create paths.
You do not have to put each part of the path as a separate item. So instead of saying
pathlib.Path.home() / "Desktop" /
"hello.txt", you could also say
pathlib.Path.home() / and then put the whole rest of the path string in here, so you can say
"Desktop/hello.txt". And that works just as well.
So whatever you consider as the most readable for you, you can use here. Remember that the
/ is a shortcut for the
.joinpath() method and that you can either chain multiple
/ operators or also
.joinpath() methods with single pieces of the path that you want to create, or you can just add a path string on the right side of this operator as well.
The important thing here is that this is only going to work if we have at least one
Path object part of the operation, which here you created by using the
.home() class method that we discussed in the previous lesson. Okay, let’s quickly sum this up.
You’ve now seen three different ways of creating
Path objects. The first one was from a string where, keep in mind, with Windows, you’ll have to do a little extra, either using forward slashes or using raw strings.
The second option you saw was using
Path.cwd(). And the third option you saw is using the
/ operator or the equivalent method,
And these are three different ways of creating
Path objects. In the next lesson, you learn how to check whether any of these paths and files that you address with
Path objects actually exist.
Become a Member to join the conversation.