• Javascript
  • Python
  • Go
Tags: time python file

Get the time a file was last modified in Python

Python is a powerful and versatile programming language, known for its ease of use and wide range of applications. One of its many useful fe...

Python is a powerful and versatile programming language, known for its ease of use and wide range of applications. One of its many useful features is the ability to retrieve the time a file was last modified. In this article, we will explore how to use Python to get the last modified time of a file and how to format it for better readability.

To get the last modified time of a file in Python, we will use the `os.path` module. This module provides various functions for working with file paths and properties. We will be using the `getmtime()` function, which returns the time a file was last modified as a floating-point number representing the number of seconds since the epoch (January 1, 1970, 00:00:00 UTC).

Let's begin by importing the `os.path` module:

```

import os.path

```

Next, we need to specify the file path for which we want to retrieve the last modified time. For the purpose of this article, let's assume we have a file named "example.txt" in the current directory. We can specify the file path as follows:

```

file_path = "example.txt"

```

Now, we can use the `getmtime()` function to get the last modified time of the file and store it in a variable named `last_modified`:

```

last_modified = os.path.getmtime(file_path)

```

The `last_modified` variable now contains a floating-point number representing the time the file was last modified. However, this number is not very readable for humans. To make it more user-friendly, we can use the `datetime` module to convert it into a more familiar date and time format.

First, we need to import the `datetime` module:

```

import datetime

```

Then, we can use the `fromtimestamp()` function to convert the floating-point number into a `datetime` object:

```

last_modified_datetime = datetime.datetime.fromtimestamp(last_modified)

```

Now, we can format the `last_modified_datetime` object to our desired format using the `strftime()` method. This method takes a format string as an argument and returns a string representing the date and time in the specified format. Here are some common format codes that we can use:

- `%Y` - Year with century as a decimal number.

- `%m` - Month as a zero-padded decimal number.

- `%d` - Day of the month as a zero-padded decimal number.

- `%H` - Hour (24-hour clock) as a zero-padded decimal number.

- `%M` - Minute as a zero-padded decimal number.

- `%S` - Second as a zero-padded decimal number.

Let's say we want to display the last modified time in the format of "Month Day, Year, Hour:Minute:Second". We can do so by using the following format string:

```

format_string = "%b %d, %Y, %H:%M:%S"

```

Then, we can use the `strftime()` method to format the `last_modified_datetime` object and store it in a variable named `last_modified_formatted`:

```

last_modified_formatted = last_modified_datetime.strftime(format_string)

```

Finally, we can print out the last modified time in the desired format:

```

print("The file was last modified on", last_modified_formatted)

```

If we run the code, we will get the

Related Articles

Python Directory Tree Listing

Python is a powerful and versatile programming language that is widely used in various industries such as web development, data science, and...