Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Deleting a File

00:00 In this lesson, you will learn how you can delete a file using Python’s pathlib module, and you can do that using the .unlink() method.

00:09 So you have this march.md file in there. It’s got a different ending than the other ones. You’re using a Markdown file here, and maybe planning up ahead until March is maybe just a little bit too far.

00:19 So let’s get rid of this file and just delete it.

00:24 Again, you will need the file path to the resource that you want to delete. So I’ll call this file_path, and it lives inside of the notes_dir in "plans" in "monthly". And then you also need the filename, which was "march.md". Reveal the file path, and let’s double check that I typed the right one by seeing whether it exists.

00:52 Okay, so this file exists currently. Now I can call the .unlink() method on it to delete it. So I can say file_path.unlink().

01:04 I don’t get anything returned from this method, but if I now check whether the path still exists, then you can see I get False because the file was deleted.

01:15 So using .unlink(), you can delete files, and if you call .unlink() on a file path that doesn’t exist,

01:26 then you will get a FileNotFoundError. So I can’t delete this file because there is no file at the end of this path. Now, if you want to avoid raising this FileNotFoundError, then you can pass an argument to .unlink() which is called missing_ok.

01:41 file_path.unlink() and then you can say missing_ok. You can already see it’s pointed out here. If I set this to True instead of the default, which is False, the FileNotFoundError won’t get raised. And in this case, nothing happens, because the resource at the file path was already deleted before.

02:05 So in order to delete a file, you can call the .unlink() method on a Path object. And to do that, you first have to define the file path that points to the resource that you want to delete and then call .unlink() on it.

02:18 And then if you check whether it still exists, you should get a False. Now, if the file doesn’t exist when you try to call .unlink() on it, then Python raises a FileNotFoundError.

02:29 And here’s an example of that, so you also tried to call .unlink() on a non-existing file path, and you got a FileNotFoundError. If you want to avoid raising this error, then you can pass missing_ok=True when you call .unlink() on a file path, and then it avoids raising the error if the resource at the end of the path doesn’t exist. And if it exists, then it’s going to delete it.

02:55 So this is how you can delete a file using Python’s pathlib module. However, you can’t use .unlink() to delete a folder, and in the next lesson you’ll learn how to do that.

Become a Member to join the conversation.