3

This is very similar question to this one, but for Python instead of powershell. It was also discussed here, and here, but no working solutions are posted.

So, is there a way to create a directory in Python that bypasses the 260 char limit on windows? I tried multiple ways of prepending \\?\, but could not make it work.

In particular, the following most obvious code

path = f'\\\\?\\C:\\{"a"*300}.txt'
open(path, 'w')

fails with an error

OSError: [Errno 22] Invalid argument: '\\\\?\\C:\\aaaaa<...>aaaa.txt'
psarka
  • 1,562
  • 1
  • 13
  • 25
  • 3
    Windows paths can be up to 32767 characters, as limited by NT's [`UNICODE_STRING`](https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wudfwdm/ns-wudfwdm-_unicode_string) counted string type, give or take a few characters for the final resolved path (e.g. "C:" -> "\Device\HarddiskVolume2"). But the length of file and directory names is limited by the file system. Almost all supported file systems limit this to 255 characters. See [File System Functionality Comparison](https://learn.microsoft.com/en-us/windows/desktop/FileIO/filesystem-functionality-comparison). – Eryk Sun Nov 08 '18 at 11:40

1 Answers1

3

Thanks to eryksun I realized that I was trying to create a file with too long of a name. After some experiments, this is how one can create a path that exceeds 260 chars on windows (provided file system allows it):

from pathlib import Path

folder = Path('//?/c:/') / ('a'*100) / ('b'*100)
file = folder / ('c' * 100)

folder.mkdir(parents=True, exist_ok=True)
file.open('w')
psarka
  • 1,562
  • 1
  • 13
  • 25