Creating Three Files
Again, Python doesn’t give me any feedback, so let’s head back over to the graphical user interface to see whether the file’s there. So I’ll navigate inside of my folder, and here it is—
"file2.txt". So this part is going to create the
Path object that points to
file2.txt, and then I’ll just directly call
.touch() on it. When I execute this, I essentially just did the same thing that I did here, but without the intermediate step of using a variable for it.
And if you remember,
.joinpath() is just the same as using this
/ (slash) operator on a
Path object and the string. So here again, I’m just creating the
Path object the same way I did up here, inside of these parentheses or up here with that operation that I assigned to a variable. And now again, I’m going to call
.touch() on it.
We just created these three files, and I used three slightly different approaches to do it. There’s none that is inherently right or wrong, but I would argue that this first approach is probably the best one because it’s the most readable. Like, this one, if you know how these
Path objects work, and you know you need to put it into parentheses here so that Python doesn’t attempt to call
.touch() on the string, which would fail. Let’s try it out.
And I would say this is not the most readable, so it’s probably not an ideal way to solve this. That’s why I think, like, using
.joinpath() in a situation like that makes a bit more sense because you can more clearly see that you’re just chaining another method call to
.joinpath(), which returns this
04:08 Okay, but different ways of doing it. None of them is wrong. But again, I would argue that probably the first one is the best one because it’s the most readable one. That’s it about creating the three files. All right, next lesson, next step.
Become a Member to join the conversation.