Get 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, and when we exceed this limit, then 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 also 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 using 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.

Find the Maximum File Name Size

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

Example

Following is the example which finds the maximum file name size using the function os.pathconf() -

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")

Here is the output of the above program -

Maximum file name length on this system: 255 characters

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

Handling Exceptions

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

Example

Here is an example of using the try-except block while using 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}")

Below is the output of the above example -

Unable to retrieve limit: module 'os' has no attribute 'pathconf'

Typical File Name Limits by File System

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

  • NTFS (Windows): 255 characters
  • ext3/ext4 (Linux): 255 characters
  • HFS+, APFS (macOS): 255 characters
  • FAT32: 255 characters

File Name vs. File Path

Here are the differences between File Name and File Path -

  • 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.
Updated on: 2025-06-08T12:56:40+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements