Create directory with path longer than 260 characters in Python

Viewed 1541

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'
1 Answers

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')
Related