Skip to content

Windows: OSError: [Errno 22] Invalid argument when trying to open a file with filename length >= 256 #130542

@fireattack

Description

@fireattack

Bug report

Bug description:

Tested with:

  • Microsoft Windows 10 Pro [10.0.19045 N/A Build 19045]
  • Python version: 3.13.0rc2 (main, Sep 9 2024, 20:49:38) [MSC v.1929 64 bit (AMD64)]
  • LongPathsEnabled already enabled in registry
  • NTFS filesystem

The problem is not about the length of the full file path (you can easily pass 500+ without any issue), but the filename.

import sys
import os

print('Python version:', sys.version)

if not os.path.exists('D:\\' + 'f' * 200):
    os.mkdir('D:\\' + 'f' * 200)

p1 = "D:\\" + 'f' * 200 + '\\' + 'a' * 200
print(len(p1))
open(p1, 'w').write('')

p2 = "D:\\" + 'f' * 200 + '\\' + 'a' * 255
print(len(p2))
open(p2, 'w').write('')

p3 = "D:\\" + 'f' * 200 + '\\' + 'a' * 256
print(len(p3))
open(p3, 'w').write('')
>bug
Python version: 3.13.0rc2 (main, Sep  9 2024, 20:49:38) [MSC v.1929 64 bit (AMD64)]
404
459
460
Traceback (most recent call last):
  File "D:\long\bug.py", line 19, in <module>
    open(p3, 'w').write('')
    ~~~~^^^^^^^^^
OSError: [Errno 22] Invalid argument: 'D:\\ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'

As you can see, Python throws a random OSError: [Errno 22] Invalid argument when filename is 256-character long.

I also tried to use R'\\?\D:\', does not help.

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions