Using the Forward Slash Operator
00:00
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, /Users/Martin/Desktop/hello.txt
.
00:39 Let’s play around with this a bit more in the IDLE REPL.
00:45
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 "hello.txt"
,
01:02
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 Path
objects.
01:17
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.
01:27
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 pathlib.Path.joinpath()
.
01:42
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 "Desktop"
.
01:58
And then I can continue, join another path, "hello.txt"
.
02:06
Running this operation with .joinpath()
is the same as using the /
operator, and you can see it produces the same output as well.
02:16
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.
02:27
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
02:43
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.
02:59
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.
03:23
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.
03:41
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.
03:53
The second option you saw was using Path.home()
or Path.cwd()
. And the third option you saw is using the /
operator or the equivalent method, .joinpath()
.
04:06
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.