Creating Three Files
With the folder created, my next task is to create three files inside of that folder and call them
image.png. The first two are
The third one is a
.png file. Okay, so I already have access to my folder, so I know I can create paths to the files—create the
Path objects first—and then also make them.
file1, and that’ll be
my_folder and then the name
".txt". There you go. And to actually create it, I’m going to need to say
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—
00:59 Let’s make the other ones.
And just for fun, let’s riff on this a little. This is probably the most clean way to create this file. Like, first you create a path and then you call the
.touch() on it.
But you can also like mix it up a little. So for example, I don’t really need to create the variable for
file2, let’s say, but I can just directly create it.
"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.
Back over to the graphical user interface. Yep.
file2.txt is also here.
And then just to play around a little more, I’m going to use another way to create the third one. I’m going to say
my_folder.joinpath(), and then
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.
And this should have created
image1.png, but let’s take a look. And here it is. And you can see also the first two are
.txt files, and the third one is a
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.
So if you do
my_folder / "test.txt" and then say
.touch() like that, you see, then Python is going to think that you want to call
.touch() on the string.
And this is not going to work out because
'str' object has no attribute 'touch'. So you need to first create a
Path object, which is why you use parentheses here.
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.