Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Once again, Python makes it easy to automate tasks. In this video let's take a look at how to search for files and directories.
Python Documentation
-
0:00
One of the benefits that directories give us is a logical grouping of files.
-
0:04
We can keep all of our cat photos,
-
0:06
project files or favorite rock operas together in one place.
-
0:10
Often, when we're creating software for working with directories in files,
-
0:13
we wanna be able to search for particular files and directories.
-
0:16
Luckily, Python makes this pretty easy for us.
-
0:18
So I've imported os.
-
0:20
I'm gonna do os.listdir, and you can see all of the files and
-
0:24
directories that are currently in this directory.
-
0:27
Those dir method gives us back everything that's in the directory.
-
0:31
By default, it uses the current working directory,
-
0:33
but we can provide it a path right here if you wanted to.
-
0:36
And it'll tell you everything that's in that path.
-
0:40
Slightly more useful though, is the scan dir method.
-
0:43
Now we're gonna pass this one to list because it gives us an iterable to consume
-
0:48
and we wouldn't see anything good if we didn't pass it to list.
-
0:51
So we can see here all of these dir entries.
-
0:53
Each one of these dir entries is an object that represents an entry in the directory,
-
0:57
so it's either a file or it's a directory or whatever.
-
1:02
What's cool about these is we can use them to get some basic information about their
-
1:05
equivalent entry without having to go back to the file system and
-
1:09
inspect a particular file.
-
1:11
For instance, let's look at this one here.
-
1:15
So, 0123.
-
1:18
Okay?
-
1:19
So, I'm going to say files=list(os.scandir).
-
1:23
And then I'm gonna say files(3).name.
-
1:27
And I get bootstrap-3.3.7-dist.zip which you can get off
-
1:31
the getbootstrap.com website.
-
1:33
So let's find out if files[3] is a file.
-
1:37
And it is, so cool.
-
1:39
So now I can get some statistics about the object by using the stat method.
-
1:44
So I can do files[3] and then I can say stat.
-
1:48
And I get these stat results.
-
1:50
Now the one of these that is the most useful,
-
1:52
the most interesting is this one over here, this ST size.
-
1:55
This is the size of the file in bytes.
-
1:58
This is really handy if you wanted to, for example,
-
2:01
flag files that are above a certain size.
-
2:04
Now one more thing to bring up.
-
2:05
Scan dir gives you a stream like iterator.
-
2:08
Like when you use the open function.
-
2:10
So if you're not consuming it right away, and then ending the block it's in.
-
2:13
Like a for loop or a function.
-
2:15
Or you're not using it with a context manager like With.
-
2:19
You'll want to call the close method on it.
-
2:21
So for instance if I had scanner = os.scandir() I would
-
2:25
eventually want to do scanner.close.
-
2:29
That would close out the scanner and free up memory.
-
2:32
Now, let me show you another way to work your way through directories.
-
2:35
We can use the os.walk method to step through all of the files and
-
2:39
directories in a particular directory.
-
2:40
This isn't exactly the same as using scandir, but
-
2:43
it gives us a handy way to explore file trees.
-
2:46
I've already started, but I'm going to finish a script here called tree.py.
-
2:51
And I say started, I've created a file.
-
2:54
Now inside here, in this directory, I have a bootstrap directory and
-
3:00
that's full of all the files that you get when you download bootstrap.
-
3:02
So, I want to look through that.
-
3:05
So, I'm gonna import OS, and
-
3:06
then I'm gonna make a new function named treewalker.
-
3:10
And it's going to start at some directory.
-
3:12
So, my total size is 0.
-
3:15
And my total number of files is 0.
-
3:18
So for the root, for the dirs and for
-
3:22
the files that are in os.walk(start) whatever the start directory is.
-
3:28
My subtotal is going to be equal to the sum of os.path.getsize(),
-
3:34
which is similar to doing the os.stat and then pulling out the ST_SIZE.
-
3:38
But getsize just pulls directly that off.
-
3:40
And I'm gonna use os.path.join, and the root, and
-
3:44
the name, and I'm gonna do that for every name that's in files.
-
3:50
And then I'm gonna say that total_size will have the subtotal added to it.
-
3:59
And I'm gonna say that file_count will be equal to the len(files).
-
4:07
Total number of files over there.
-
4:09
And then total files is going to plus equal the file count.
-
4:12
Now I could of course just combine those two lines but
-
4:14
sometimes it's nice to print the whole thing out.
-
4:17
So,I'm gonna print root and then consumes.
-
4:24
And I'm gonna end that with a space, and
-
4:27
then I'm gonna print the subtotal, and end that with a space.
-
4:33
And then I'm gonna print bytes in, and then the file count,
-
4:39
and I'm gonna say non-directory files.
-
4:44
And then out here outside of the for loop, I'm gonna print start
-
4:49
contains and then total_files.
-
4:55
And then files with a combined size of,
-
4:59
and then total size, and then bytes.
-
5:06
So a lot of stuff to do there.
-
5:08
But then I'm gonna call treewalker down here with Bootstrap.
-
5:12
There we go, save that, come back over here, and
-
5:16
I'm gonna execute tree.py.
-
5:20
So Bootstrap has zero bytes, and zero non directory files.
-
5:23
Bootstrap css consumes 1.3 million bytes in 8 non-directory files.
-
5:30
Bootstrap fonts contains 215,000 bytes.
-
5:34
Bootstrap.js is 117,000.
-
5:37
Bootstrap in total has 16 files with a combined size of 1.6 million bytes.
-
5:44
So that's pretty cool.
-
5:46
That's a really nice way just to kind of quickly see what's going on.
-
5:49
There's a lot more to the OS module, and
-
5:51
we're going to explore some more of it in the next two sections.
-
5:55
Feel free though to explore it a bit more on your own.
-
5:57
It's one of the more interesting modules in Python, especially since dealing with
-
6:00
differences and operating systems is often such a thorny area.
-
6:02
All right, take a little break, have a snack or stretch, and then come back to
-
6:06
learn about manipulating files with Python, instead of just looking at them.
You need to sign up for Treehouse in order to download course files.
Sign up