Deleting a File
I do have
file1 in here, and I want to get rid of it. So first, I will need the path. This path, I need to get it. Probably the most readable way to get the path to
file1.txt in here is going to be to say
file1 = my_folder and then
And that should be the same path as we have here on position one. Let’s double-check with a little, having a little fun with Python on the side, so I’m going to say
list(my_folder.iterdir()), and I want the second element in here.
02:01 Again, Python doesn’t really give me a feedback here whether or not this worked. You’ll see that if I run it and the file doesn’t exist, it actually gives me feedback and tells me that this file doesn’t exist.
and there it is. It’s gone. Not here anymore. And of course, we want to go double-check in the graphical user interface again. So if I head over here and go back to
my_folder/, now there’s another gaping hole where
file1.txt used to be, so we successfully deleted it. Awesome.
Become a Member to join the conversation.