Deleting Files and Directories
FileNotFound if the file doesn’t exist. And the reason that there are two identical functions with different names is just a question of compatibility with different programming paradigms, some people in old or, kind of, Unix systems like to use
unlink() as their preferred nomenclature, whereas some other people in different systems use
01:11 The sample directory that I’ll use for both the deleting files and the deleting directory sections of this lesson will be the same. I have a couple of subfolders with files within them, and then I just have a couple of files in the top-level sample directory.
01:37 I have a couple of text files in here, and then I have a couple of folders here I could show you, as well, just to make totally sure that these folders have what we expect to be in them: a few Python files, and the other folder has some stuff in it too.
And, as you can see,
os.listdir(), both of those text files are gone. All I have left is a few folders. And now, just to demonstrate, I want to show you here, as you can see,
os.remove() takes in a
path parameter, then a
dir_fd parameter, which I won’t get super into but you can check that out in the documentation then
I also want to show you that if I try to call—let’s do
".unlink()", just for variety—if I try to call this on
"folder_2", let’s say,
"folder_2", just to get that some touch there, then I’ll get an
Operation not permitted, because the
unlink() function is not built to work on directories, so this operation will not be allowed.
If the directory is non-empty, though, it raises an
OSError, because it doesn’t want you to accidentally delete full directories. It wants to make sure that they’re cleared out before you delete them.
If you need to delete non-empty directories, or an entire directory tree, you can use
shutil. You have to import
shutil, which is short for shell util, and then it has an
rmtree() function, where you can call that on a directory path and it will delete the entire directory tree rooted at this path.
05:09 So, if you have some subfolders inside this directory that you’re calling it on, those will all be deleted, too, no matter if they’re full or empty. So, this is kind of the nuclear option, of deleting directories in Python.
05:21 Again, here’s the sample directory that I’m using. Let’s go check it out in the REPL. All right, let’s delete some directories. So first, I’ll show you the filesystem here, as you can see, I’ve replenished it from earlier in this lesson when I deleted a bunch of stuff.
Now it’s back to normal. So, let’s make a first try here and just see what happens if you call
'folder_1'. And, of course, I have to put that in strings because it needs to be the path rather than—there’s no variable called
So, that’s not going to work. If I want to actually delete something using
os.rmdir()—I’m going to have to really quickly just make a new directory, which I’ll just call
'test'—and then I’ll have to
listdir() to show you that it’s in there.
So, now there’s a
test/ directory, and this
test/ directory is, in fact, empty, so it can be deleted. This might seem like an inconvenient feature, that you can only delete, with these basic things, directories that are empty.
06:27 But really, it’s just a safety feature. It doesn’t want you to delete stuff when there might be useful files within that directory. It doesn’t want you to delete that without, at least, thinking about what’s in it.
Now, though, I can say
os.rmdir('test'), and now you can see it’s back, it’s gone again. But I’m going to create it one more time to show you that I can now do the same thing with the
I can say
dir_path = Path('test'), and then I can say
dir_path.rmdir(), and now I’ll take a look again. I created it here, and then I created a
Path object, then I removed it, and look, it’s back to gone again.
and then I’ll just quickly show it to you, so I’ll list
folder_2. It has this
'newdir' in it, along with these Python files. If I do, now,
shutil.rmtree('folder_2') then it’s still totally gone and that subdirectory, it was also deleted.
08:00 So this really is the heavy-hitter option, which just deletes everything without caring what’s in it. And so this can be really useful if you know that everything in your folder is garbage that you can delete.
08:09 But if you don’t know that, you should be really careful and try to use these other functions that will warn you by throwing an error, if you’re trying to delete something that has useful things in it.
08:20 So, that’s how you can delete files and directories in Python after you’ve learned to create them, and all that sort of stuff. In the next lesson, I’ll cover how to search for specific files and directories in your filesystem.
08:32 And this works in really well with deletion because, as I was saying, you want to be really careful about what you delete. So learning filename pattern matching will be a really helpful tool in making sure that you’re really careful about your file operations.
Become a Member to join the conversation.