Os path getctime

In Python, you can use the standard library os and pathlib modules to get timestamps such as the creation, modification, and access date and times of files. It can be obtained in Unix time (Epoch time, Posix time) but can be converted to date and time using the datetime module.

  • os.stat_result — Miscellaneous operating system interfaces — Python 3.10.0 documentation

You can get the following timestamps. The meaning differs depending on the OS, so be especially careful about the creation time.

  • atime: Access time
  • mtime: Modification time
  • ctime: Change time (Unix) and creation time (Windows)
  • birthtime: Creation time (Some Unix in the FreeBSD family, including macOS)

This article describes the following contents.

  • Get os.stat_result with timestamp information
    • Path.stat()
    • os.stat()
    • Attributes of os.stat_result
  • Get timestamps with the os.path function
  • Convert timestamp to datetime
  • Type of timestamp: atime, ctime, mtime, birthtime
  • Get the modification date and time
  • Get the creation date and time
    • Cross-platform approach

As an example, create a file and update it after 10 seconds.

Get os.stat_result with timestamp information

You can get file metadata, such as timestamps, as attributes of the os.stat_result object.

  • os.stat_result — Miscellaneous operating system interfaces — Python 3.10.0 documentation

Path.stat()

You can get the os.stat_result object with the stat() method of the pathlib.Path object.

os.stat()

You can also use the os.stat() function of the os module to get the os.stat_result object. The argument can be a path string or a pathlib.Path object (Python 3.6 or later).

All methods give you the same os.stat_result object.

Attributes of os.stat_result

You can get timestamps with the attributes st_atime, st_mtime, and st_ctime of the os.stat_result object. On some Unix systems of FreeBSD family, including macOS, there is also an attribute st_birthtime. The meaning of each is described later.

Read more: Funny cooking show

All attributes are floating-point numbers and represent Unix time (Epoch time, Posix time). How to convert this value to datetime is described later.

There are also st_atime_ns, st_ctime_ns, and st_mtime_ns which store the value of an integer int in nanoseconds. There is no equivalent attribute for st_birthtime.

Note that, as shown in the official documentation, floating point number float contains information after the decimal point, and xxx_ns stores values in nanoseconds, but the accuracy is not guaranteed.

The exact meaning and resolution of the st_atime, st_mtime, and st_ctime attributes depend on the operating system and the file system. For example, on Windows systems using the FAT or FAT32 file systems, st_mtime has 2-second resolution, and st_atime has only 1-day resolution. See your operating system documentation for details. os — Miscellaneous operating system interfaces — Python 3.10.0 documentation

os.stat_result has various other attributes, such as st_size, which indicates the size of the file in bytes. See the following article on getting the size of files and folders.

  • Get the size of a file and directory in Python

Get timestamps with the os.path function

You can also get timestamps with the os.path function, getatime(), getmtime(), and getctime().

  • os.path — Common pathname manipulations — Python 3.10.0 documentation

In Python 3.6 or later, you can also specify pathlib.Path object as an argument instead of a path string.

As you can see in the source code, these functions just get the corresponding attributes of os.stat_result.

Of course, you can get exactly the same value as getting os.stat_result with Path.stat() or os.stat() and specifying its attribute.

Read more: Black and emerald green hair

Functions to get st_atime_ns, st_ctime_ns, st_mtime_ns and st_birthtime are not provided.

Convert timestamp to datetime

As in the previous sample code, the timestamp is expressed in Unix time (Epoch time, Posix time).

Read more: Printable quinceanera checklist

To convert it to a datetime object, use the datetime.fromtimestamp() function of the datetime module.

For more information, including how to specify the time zone when converting, see the following article.

  • Convert between Unix time (Epoch time) and datetime in Python

The datetime object can be converted to a string in any format or ISO format.

Type of timestamp: atime, ctime, mtime, birthtime

As mentioned in the introduction, there are different types of timestamps: atime, ctime, mtime, and birthtime.

  • atime: Access time
  • mtime: Modification time
  • ctime: Change time (Unix) and creation time (Windows)
  • birthtime: Creation time (Some Unix in the FreeBSD family, including macOS)

See the following page for details.

  • MAC times – Wikipedia

Get the modification date and time

For the so-called modification time, you can get mtime regardless of the OS.

Use the st_mtime attribute of os.stat_result or the os.path.getmtime() function as in the sample code so far.

Read more: Printable quinceanera checklist

To convert it to a datetime object, use the datetime.fromtimestamp() function of the datetime module.

On Unix, you can use ctime to get the last modified time of metadata, so if you want to detect file name changes, for example, use ctime instead of mtime. Note that on Windows, ctime is the creation time.

Get the creation date and time

As mentioned above, the method of getting the creation time varies depending on the OS.

  • Windows: ctime
  • Some Unix such as macOS: birthtime
  • Other Unix: The creation time cannot be retrieved

Cross-platform approach

If your program is only for Windows or macOS, you can use st_ctime or st_birthtime, but if you want to support multiple platforms, you should define a function.

Cite the sample code listed in the following question and answer on Stack Overflow.

  • How to get file creation & modification date/times in Python? – Stack Overflow

First, it checks whether the system is Windows or not with platform.system(), and then it uses exception handling to switch the operation depending on whether the st_birthtime attribute exists or not.

See the following article for more information about platform.system() and exception handling.

  • Get the OS and its version where Python is running
  • “try … except … else … finally …” in Python

The argument can be a path string or a pathlib.Path object (Python 3.6 or later).

Note that the function in this sample code returns st_mtime, which indicates the modification time, if st_birthtime does not exist. In some cases, it may be better to return None to indicate clearly that the creation time cannot be retrieved.

Related Posts