Getting File Attributes
00:00 Now you’ve got a list of all the files in your current directory, but what happens when you want to actually get the attributes of those files? Like, the last time they were modified, or when they were created, or their size, or their permissions, or something like that?
The first is
os.stat(), which takes in a path as a string and then returns a
stat_result object that has that file data in it. So that
stat_result object is something where you can access fields of that object and get the file data.
The objects in those iterators all have
.stat() methods that return the same data as
os.stat(). So again,
os.stat() is something that you can use independent of these iteration paradigms, whereas
Path.iterdir(), they package this
.stat() data up with the objects that they actually are iterating through.
01:33 The sample directory that I’ll use for this is the same as in the last lesson. It has a couple of subdirectories with a few files in each, and then has a few files just in the top-level directory.
01:43 So let’s take a look at this over in the REPL. Okay, file data: how do you get it? Well, let’s try this. First, I just want to get the contents of this directory, and as you can see, there are a lot of files to choose from.
02:19 But as you can see, you have the mode, which is the permissions associated with that object. You have a unique identifier for the object. You have all sorts of different things: the number of links, the creation and modification time.
02:30 Some of these will even differ by system. On Windows, on Linux, on Mac, some of these will have different values. You really have to be careful and understand your system well to work with these.
In order to show you just a little bit about how you can actually access these kinds of things, I’m just going to focus on the
st_mtime, the modification time—last-modified time, I should say—just so that I can have one thing to show you as I go through with these different methods of getting statistics for objects.
03:11 If you’re not familiar with that representation, I would encourage you to check out the Real Python tutorials on the topic. But the gist of it is, I’ll have to convert that if I want a more readable time.
I’d have to say something like
time.ctime(os.stat("file1.py").st_mtime). So, there’s a fair amount of work that you’ll need to do here, if you want to actually convert these into nice, readable, human-intelligible formats.
03:43 Now that you know how to, at least, get these things and convert them, a little bit, into a more readable format, I’ll show you the other two ways that I want to talk about to actually get this data.
I could say
for obj in os.scandir() on the current directory, and then let’s make it a little bit more informative output. Let’s say something like
mod_time = obj.stat(), that’s how you get the statistics for a
04:39 And as you can see, most of them were actually at the same time, because I created these all for the purposes of this lesson, but then one of them I modified just recently to give at least some variety.
So in reality, what I’m doing is I’m calling the
.iterdir() method of the
dir_path object, would be a better way to say that. But this does exactly the same thing and it even has the same method names, so that’s super convenient for when you’re working with these.
05:21 Really, these two function in almost exactly the same way. So, that’s three different ways to get statistics from files using Python. In the next lesson, I’ll cover something I haven’t covered yet, which is how to create directories.
Become a Member to join the conversation.