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

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds 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 the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

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?

00:15 That’s the topic of this lesson.

00:18 Just like in the last lesson, there are three different options for getting file information: two from the os module, and then one from the pathlib module.

00:28 And in fact, I say there are three different options, but there are many more than just these. These are just the three that I’m going to show you today.

00:35 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.

00:52 Then os.scandir() and pathlib.Path.iterdir(), as I mentioned in the last lesson, provide this information within the objects that they return in their iterators.

01:04 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 os.scandir() and Path.iterdir(), they package this .stat() data up with the objects that they actually are iterating through.

01:25 So it’s more convenient when you’re doing iteration, but os.stat() might be more convenient if you just need a one-off, “What’s in this file?”

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.

01:56 And the first method that I looked at in the slides was the os.stat() function. That just takes in a file path, a filename, and gives you an os.stat_result object as the results.

02:10 Now, this stat_result object has a lot of different fields, and I can’t really get into all of them because there’s many more than are shown here, and it’s just a wealth of information.

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.

02:41 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.

02:58 So, as you can see, if you call this, you can then directly access those fields. So I could say st_mtime, and this is a time represented as floating-point seconds.

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.

03:21 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:36 That’s just because this data is stored, really, at the system level, where everything is just bare numbers.

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.

03:52 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 scandir() object.

04:10 And then I can say .st_mtime, and now I can say something nice, like f"File {obj.name} was last modified at", and then I could say, time.ctime() of the mod_time.

04:29 And so this should give us everything in a nice readable format. And in fact, you can see that that’s what it does. It tells you each filename and the time that they were last modified.

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.

04:48 Then the pathlib.Path() option here works almost exactly the same way, except you have to create the Path object first, before you can start iterating through it.

04:59 So I create the Path object, and then I can really pretty much just copy and paste. I just have to call the .iterdir() function on the dir_path object.

05:08 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.

patientwriter on Aug. 25, 2020

I have been using os.stat(). Did not know about the other two. Great info! Thanks.

Liam Pulsifer RP Team on Aug. 25, 2020

Glad to hear you got something out of this lesson @patientwriter! Best wishes for your continuing Python journey :)

Become a Member to join the conversation.