Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Often we find it necessary to delete files and directories. We'll look at some ways to accomplish these frequent and important tasks.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
I've found in my Python travels that I
usually care more about deleting files and
0:00
directories, than I do moving them around.
0:04
In fact, I might have done more deletion
than creation even since, quite often,
0:06
files get added through
version controller or
0:09
user uploads, and
then I need to clean them up with Python.
0:11
So let's look at how to delete files and
directories with Python.
0:14
But be warned,
Python deletes items immediately.
0:17
The files won't be moved to the users
trash or any equivalent location.
0:20
We'll look at a handy third-party library,
though, named Send2Trash,
0:23
that will help us avoid this behavior.
0:26
We're gonna look at three different
functions for removing things and,
0:29
unfortunately, you kinda have
to know all three of them.
0:33
They each do different things and
0:36
you'll often need to use at
least two of them together.
0:37
Let's start with the function
that lets us delete files.
0:41
So I've created a file here
in the JavaScript directory
0:43
that's called treehouse.js.
0:46
There's nothing in there, it's just a file
I wanted to have on hand to delete.
0:48
So now I can do os.remove, and
0:51
I can say bootstrap/js/treehouse.js.
0:55
And if I look now, the file's gone.
1:01
The remove function lets us,
well, remove a file.
1:04
Now if you're using Windows and
1:07
you try to remove a file that's currently
in use, Python is going to throw an error.
1:08
On POSIX systems, it'll remove the
directory entry so it wouldn't show up if
1:12
you listed the directory, but it doesn't
clear away the associated memory.
1:16
So the memory is still in use but
the file doesn't show up in the directory.
1:20
That can only be used to remove files.
1:23
So what are directories?
1:27
Well, Python requires that directories
be empty before you try and delete them.
1:28
So you'll probably find yourself
using os.remove first and
1:33
then you can bust out os.rmdir
to remove a directory.
1:38
So, for instance, I know that
the bootstrap/img directory is empty.
1:41
Let's double check that.
1:47
And it is because it doesn't even exist.
1:49
Let's create a new folder here and
we'll call it img.
1:51
So there's nothing in there.
1:53
And now if we remove it, it's gone.
1:55
Great, the directory is empty,
Python is happy to delete it.
1:58
But if I try to do os.rmdir bootstrap/js,
which we just saw
2:01
has several files in it, I get an error
saying that it doesn't want to do that.
2:07
I also misspelled bootstrap.
2:13
There we go, there's our OSError
of the directory isn't empty.
2:16
That says the js directory isn't empty,
Python complains like I just showed you.
2:20
We could use os.walk or os.scandir
to move through the directory and
2:24
delete everything before we
remove the directory itself.
2:28
We could do, say, for thing in os.scandir,
2:31
bootstrap/js/, if thing.is file,
so if it's a file,
2:36
Then os.remove, thing.path.
2:45
Thing.path gives us the path to the file.
2:50
And now we can do os.rmdir,
bootstrap/js, and
2:53
the directory happily is deleted.
2:57
So, thanks Python.
3:01
Now if I need to delete a tree of
empty directories I can use os.rmdirs.
3:03
Let go ahead and make one first,
let's do os.makedirs,
3:08
let's do bootstrap/js/packages/stuff, so
3:13
if you had a whole bunch
of directories there.
3:17
And now we can delete that,
we can do os.removedirs,
3:21
and we can give it
bootstrap/js/packages/stuff.
3:26
But that's kind of weird, right?
3:31
I said to remove stuff, and
3:33
it removed stuff, it removed packages,
it removed js, but it didn't
3:36
remove bootstrap because I'm currently
in the bootstrap directory right here.
3:41
This function tries to remove every
directory that you specified.
3:45
And it stops as soon as it
encounters an error though.
3:48
And usually that error is because
the parent directory isn't empty.
3:51
In our case it's because
bootstrap wasn't empty.
3:55
So Python stops and
bootstrap sticks around.
3:57
All right, so
we can delete files and directories.
3:59
But, like I said at the beginning
of the video, they're gone, gone,
4:01
they're deleted immediately.
4:04
We can prevent that by using
the Send2Trash package.
4:06
I'm gonna hop out of Python here and
I'm gonna do pip3 install send2trash.
4:10
And then I can use it in Python.
4:19
So I'll clear my screen,
and I'm gonna say,
4:22
from send2trash import send two trash.
4:26
And then I'll do, send2trash, tree.py.
4:30
And if I look in my trash can, I have
tree.py right there in the trash can.
4:34
I didn't want to delete it, so
it's in my trash, that's great.
4:42
I can bring it right back if I need to.
4:45
So, let's go up a directory here and
I want tree.py to be right there.
4:50
Often, when you find yourself needing
temporary files or directories,
4:56
you resort to a lot of things
we've just been doing.
4:59
You'll create a directory or a file,
fill it with information and
5:01
then move it to its final location or
delete it.
5:03
Well, you know Python,
if there's something that's commonly done,
5:06
that pattern is probably
built into the language.
5:09
Temporary files and
directories are no exception.
5:11
So after this temporary break I'll be
back to show you how to create transient
5:14
files and directories in Python.
5:17
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up