- [Instructor] Sometimes you need to do some work with files that goes beyond just reading and writing data. For example, you might need to find out information about a file, whether it exists, what the path of the file is, whether a given path is a file or a directory and so on. Python provides path-related utilities to help you do this. And, that´s what we´re going to examine now. In your editor, open up ospathutils_start and you can see that I´ve already imported the modules we´re going to need to work on this example. Now some of them you might already recognize from previous examples, and here's a new one named OS.
This module gives us the ability to work with Operating System related features. Let's start with something really simple. Let's print the name of the OS. So I'll just simply print os.name. All right, let's save and run that. And you can see the name of the OS is being printed here. And of course this might be different for you based on the OS that you are running this code on. Next, let's take a look at some of the path-related features.
Which is why I'm also importing the Path module from the OS class here. So, let's write some code to see if an item exists. And I'll write print and I'll say Item exists plus str and path and I'm going to use the exists function to ask if a item exists. And I give it a relative path here so I'm just going to pass in textfile.txt.
And you can see that from the previous example, we've got texfile.txt in here. Now, if you didn't run the previous lesson, then you might not have this file so you might want to go back and do that if you don't have it. But you notice I don't need to give it any path information because it's in the same directory as the script that's being run. All right, so let's just check to see if it exists and let's also see whether it is a file and in this case I'll use path.isfile and one more example.
I'll check to see if the item is a directory and obviously, it can´t be both but we'll just run the test. And I'll use the isdir for that. All right. So, in each of these cases I'm operating on the textfile.txt item. So, let's go ahead and run this code and see what happens. So, back to the debugger, and let's clear out that and let's run this. All right, so you can see that the item exists and it is a file and it is not a directory.
Let's try a few more operations. So, I'll clear that. Let's get the items full path. And then I'll cast this to a string and I'll call path.realpath and I'll just pass in the local path here so, textfile.txt and let's also use the split function to separate the filename from the path. So in that case, I'll call path.split and I´ll pass in path.realpath.
Once again, that's for textfile.txt. Okay, so now let's comment out some of the previous examples. Just to keep the output readable. All right. So, I´ll save, and we'll run. And you can see that I've been given the full path in the first example here, to that example. And then, I've also got a tuple, which contains just the path portion and the filename portion.
So now, let's get some information about the files modification time. To do that, I will write t=time and I'm going to do this by using the time classes ctime function to convert the modification time into a real time. So I'm going to write time.ctime and I'm going to use the path's getmtime function. So that gets the modification time of the file.
And I'll write, again, textfile.txt. And then I'll just print out that time. And I'm also going to use the get modification time function to construct a datetime object using the fromtimestamp function that's built-in to the datetime class. So I'll write print and I'll use datetime.datetime.fromtimestamp and path.getmtime so I'm getting the modification time here of the textfile.txt.
So just two different ways of accomplishing essentially, the same thing. Let's go ahead and run this. And you can see the modification timestamp on two different lines here. Just two different ways of formatting the time that the file was last modified. One more example, and this time, it's going to involve some date math. And if you watch the chapter on manipulating dates, then this might be a little bit of review for you. So I'm going to calculate how long ago the item was modified.
So let's go ahead into the code and do that. And let me comment out some of these guys. There we go. All right. So, I'm going to write td = and I'm going to do datetime.datetime.now and I'm going to subtract off the fromtimestamp value and again, I'm going to call path.getmtime for the textfile, all right.
And then I'm going to print It has been and I'm going to cast this to a string the td value there, that's the time difference. Since the file was modified. And I'll print it another way, I'll say print OR and then once again I'll cast to a str but this time I'll use td.totalseconds and just close off the string there, all right.
So, my variable td and I'm using the current date and time to get the now time. And I'm going to subtract off using date math the value of the modification time of the file. And I'm going to use the fromtimestamp method on the datetime class. So this gives me a timedelta object which, again, we saw earlier in the course. And this represents the difference between now and when the file was last modified. Then I'm going to use the totalseconds function on the timedelta object to get the time difference as seconds.
So let's go ahead and run this. And you can see that it's been 17 minutes and 44.259835 seconds since the file was modified. Another way of saying that is 1064 seconds.
- Installing Python
- Choosing an editor or IDE
- Working with variables and expressions
- Writing loops
- Using the date, time, and datetime classes
- Reading and writing files
- Fetching internet data
- Parsing and processing HTML