How to get the maximum file name length limit using Python?

In Python, when performing file operations such as creating or renaming files, one of the limitations is the maximum length limit of a file name. When we exceed this limit, it results in errors such as OSError: [Errno 36] File name too long.

Using os.pathconf() Function

In Python, the os.pathconf() function is used to get system-specific configuration values related to files and directories. This function helps in checking limits such as the maximum file name length or the maximum path length allowed by the file system.

Syntax

Following is the syntax of the os.pathconf() function −

os.pathconf(path, name)

Where,

  • path: The directory or file path.
  • name: The constant string key, such as 'PC_NAME_MAX', which represents the configuration value to retrieve.

Finding the Maximum File Name Size

We can use the os.pathconf() function with the constant PC_NAME_MAX to query the maximum number of characters permitted in a single file name. Most commonly, the limit is 255 characters on modern file systems.

Example

Following example finds the maximum file name size using the os.pathconf() function −

import os
import platform

def get_filename_limit():
    system = platform.system()
    
    if system == "Windows":
        return 255  # NTFS file name limit
    elif system in ("Linux", "Darwin"):  # Darwin = macOS
        try:
            return os.pathconf('.', 'PC_NAME_MAX')
        except (AttributeError, OSError, ValueError):
            return 255  # Fallback
    else:
        return 255  # Generic fallback for other OSes

# Example usage
limit = get_filename_limit()
print(f"Maximum file name length on this system: {limit} characters")
Maximum file name length on this system: 255 characters

Note: On Windows, os.pathconf() function doesn't work. In such cases, we can use the platform.system() method along with appropriate fallback values.

Handling Exceptions

Not all operating systems may support every configuration value, so it's best practice to include a try-except block. This helps avoid unexpected errors if a key like PC_NAME_MAX isn't available.

Example

Here is an example using the try-except block with the os.pathconf() function −

import os

try:
    name_max = os.pathconf('.', 'PC_NAME_MAX')
    print(f"Allowed file name length: {name_max}")
except (ValueError, AttributeError, OSError) as error:
    print(f"Unable to retrieve limit: {error}")
    print("Using default limit: 255 characters")
Unable to retrieve limit: module 'os' has no attribute 'pathconf'
Using default limit: 255 characters

File System Limits Comparison

Following are the standard limits for file names on popular file systems −

File System Platform File Name Limit
NTFS Windows 255 characters
ext3/ext4 Linux 255 characters
HFS+, APFS macOS 255 characters
FAT32 Cross-platform 255 characters

File Name vs. File Path

It's important to distinguish between file name and path limits −

  • File Name Limit: The number of characters allowed in a single file name, such as data_report.csv.
  • Path Length Limit: The total length of the full directory path plus file name, such as D:\Tutorialspoint\Articles\data_report.csv.

Conclusion

Use os.pathconf() with PC_NAME_MAX to get file name limits on Unix systems. For Windows or when the function isn't available, use a fallback value of 255 characters, which is the standard limit across most modern file systems.

Updated on: 2026-03-24T18:43:49+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements