How to get file information in Python

Obtaining information about a file in Python is an essential operation when working with the file system, as it allows you to retrieve details such as size, type, permissions, and modification timestamps. Python offers several libraries and functions to perform these operations, particularly through the built-in modules os, os.path, stat, and pathlib. In this article, we will explore the most common ways to obtain information about a file.

The os module is a powerful tool for interacting with the operating system, and the os.path subpackage contains useful functions for handling files and directories.

To check if a file exists, you can use the os.path.exists() function:


import os

file_path = 'file.txt'
if os.path.exists(file_path):
    print("The file exists.")
else:
    print("The file does not exist.")

To get the size of a file, you can use os.path.getsize():


file_size = os.path.getsize(file_path)
print(f"The file size is: {file_size} bytes")

The os module allows you to get temporal information about a file, such as the creation, modification, and access dates.


import time

mod_time = os.path.getmtime(file_path)
print("Last modification:", time.ctime(mod_time))

access_time = os.path.getatime(file_path)
print("Last access:", time.ctime(access_time))

create_time = os.path.getctime(file_path)
print("Creation date:", time.ctime(create_time))

The stat module provides greater flexibility in managing file information. Using os.stat(), you can obtain an object containing various detailed information.


import os
import stat

file_info = os.stat(file_path)
print("File size:", file_info.st_size, "bytes")
print("Last modification:", time.ctime(file_info.st_mtime))
print("Last access:", time.ctime(file_info.st_atime))
print("Creation date:", time.ctime(file_info.st_ctime))

In addition to size and timestamps, os.stat() also returns information on permissions and other file properties.

To check for read, write, and execute permissions, you can use constants defined in the stat module.


if bool(file_info.st_mode & stat.S_IRUSR):
    print("The file is readable.")
if bool(file_info.st_mode & stat.S_IWUSR):
    print("The file is writable.")
if bool(file_info.st_mode & stat.S_IXUSR):
    print("The file is executable.")

Starting with Python 3.4, the pathlib module has become a powerful and more intuitive alternative for working with file paths. This module simplifies many operations that would be more verbose using os or stat.

To obtain information about a file, you start by creating a Path object for the desired file.


from pathlib import Path

file_path = Path('file.txt')

With pathlib, checking if a file exists, whether it is a file or a directory, and other similar operations become simpler and more readable:


if file_path.exists():
    print("The file exists.")
    if file_path.is_file():
        print("It is a file.")
    elif file_path.is_dir():
        print("It is a directory.")

Retrieving the file size and various timestamps is also straightforward:


print("File size:", file_path.stat().st_size, "bytes")
print("Last modification:", file_path.stat().st_mtime)
print("Last access:", file_path.stat().st_atime)
print("Creation date:", file_path.stat().st_ctime)

pathlib also allows you to get timestamps in datetime format, making everything simpler and more readable:


from datetime import datetime

mod_time = datetime.fromtimestamp(file_path.stat().st_mtime)
print("Last modification:", mod_time)

Although pathlib does not have direct support for checking permissions, you can achieve the same result using file_path.stat().st_mode and stat:


import stat

if file_path.stat().st_mode & stat.S_IRUSR:
    print("The file is readable.")
if file_path.stat().st_mode & stat.S_IWUSR:
    print("The file is writable.")
if file_path.stat().st_mode & stat.S_IXUSR:
    print("The file is executable.")

Conclusion

In Python, retrieving file information can be done using os, stat, or pathlib, each of which has its own advantages. The os module is widely compatible and standard, while stat provides an in-depth view of permissions and other advanced properties. However, for most modern operations, pathlib is preferred for its readability and simplicity.

Back to top