Loading video player…

Creating Three Files

00:00 With the folder created, my next task is to create three files inside of that folder and call them file1, 2, and image.png. The first two are .txt files.

00:12 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.

00:25 I have file1, and that’ll be my_folder and then the name "file1"whoops—".txt". There you go. And to actually create it, I’m going to need to say .touch().

00:44 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—file1.txt. Great.

00:59 Let’s make the other ones.

01:02 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.

01:13 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.

01:23 I could say my_folder

01:28 and then "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.

01:53 Back over to the graphical user interface. Yep. file2.txt is also here.

02:01 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 "image1.png".

02:18 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.

02:43 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 .png file.

02:57 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.

03:24 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.

03:38 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.

03:47 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 Path object.

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.