0

I have read all the previous answers on StackExchange and on Google, but they don't seem to solve my problem.

Long paths in Python on Windows

Python long filename support broken in Windows

Long paths for python on windows - os.stat() fails for relative paths?

Windows path too long

I am adding '\\?\' in front of my filename, but it does not help. Here is my MWE:

test_path = '\\\\?\\d:\\' + ''.join(['a']*260)
print(test_path)
print(len(test_path))
with open(test_path, 'w'):
    pass

Just trying to save a long file on d: fails with

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-13-1ec52b105f8b> in <module>
      3 print(test_path)
      4 print(len(test_path))
----> 5 with open(test_path, 'w'):
      6     pass

OSError: [Errno 22] Invalid argument: '\\\\?\\d:\\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'

Shorter filenames go through just fine. I am using Python 3.7. What am I doing wrong?

Without '\\?\' the command fails with FileNotFoundError, as expected.

Dr_Zaszuś
  • 546
  • 1
  • 7
  • 15
  • Unrelated, but you should use raw strings for Windows file paths, this way you don't need to escape the backslashes. `test_path = r'\\?\d:\'` – Tomalak Jun 19 '20 at 12:05
  • @Tomalak I don't know about whether I "should" (the approaches are equivalent), but thanks, I am actually doing that. Just simplified things for the MWE. – Dr_Zaszuś Jun 19 '20 at 12:14
  • 1
    Of course it's your call. :) – Tomalak Jun 19 '20 at 12:21
  • 1
    @Tomalak, `r'\\?\d:\'` is invalid syntax. For whatever reason, a raw string in Python allows backslash to escape an embedded quote, but with the backslash retained (e.g. `r"a\"b"` is `'a\\"b'`). Thus a raw string cannot end with an odd number of backslashes, since the odd one out escapes the closing quote. Doubling the final backslash won't work either (e.g. `r'\\?\D:\\'`). It's valid Python syntax, but an extended path is verbatim and doesn't allow repeated backslashes. You'd have to absurdly use `r'\\?\D:\\'[:-1]` -- instead of just `'\\\\?\\D:\\'`. – Eryk Sun Jun 19 '20 at 23:19
  • 1
    `'a' * 260` is too long for a component name. The maximum *component name* length in Windows filesystems is 255 characters or less. It's 255 for NTFS, ReFS, FAT32, and exFAT. The maximum *path name* length, where a path contains a device name and one or more component names, is 32,767 characters, but a bit less in practice since DOS device names expand to longer native device names (e.g. "C:" -> "\Device\HarddiskVolume2"). – Eryk Sun Jun 19 '20 at 23:22
  • @ErykSun Oh yeah, you're right. Stumbled over this oddity before, didn't think about it when I wrote the comment. – Tomalak Jun 21 '20 at 01:35

0 Answers0