0

I am trying to open multiple files on Windows 10 with PIL. Each of these files have a very long hash combinations as their name. Storing them directly in:

C:\\Users\\my-user-name\\PycharmProjects\\MyProject\\.........

works, but as soon as I place them in one additional sub level, e.g.:

C:\\Users\\my-user-name\\PycharmProjects\\MyProject\\sub-folder\\.........

it is no longer possible to open them because the string becomes to long.

According to Long paths in Python on Windows I tried to add \?\ in front of the path, e.g.:

f = '\\?\C:\\Users\\my-user-name\\PycharmProjects\\MyProject\\sub-folder\\...' Image.open(f)

but unfortunately it does not work for me.

Any suggestions?

Niklas
  • 121
  • 1
  • 8
  • 1
    You can check this [answer](https://stackoverflow.com/a/29558658/7546950) on a similar issue: – Youcef4k Jul 08 '19 at 12:17
  • Well It appears I messed up the \\?\ in front of my path string, it worked by using `extended_path = "\\\\?\\%s" % d` and then `Image.open(extended_path)` – Niklas Jul 08 '19 at 12:17
  • FYI You can use Python's "raw" strings to prevent any backslash-escape sequences to be changed, which can make stuff like this (and regular expressions) a bit more readable. E.g. `r"\?\C:\Users\my-user...."`. – Tom Dalton Jul 08 '19 at 12:33
  • Use `r"..."` - raw string doesn't need escaping, you'll only need to use backslashes once. – h4z3 Jul 08 '19 at 12:33
  • @TomDalton, the string needs to be Unicode in order to support extended paths prior to Windows 8 (the system's ANSI -> Unicode decoding used to use a static `MAX_PATH` buffer) and also because a Windows filepath should be Unicode anyway. This means *in Python 2 we cannot use a raw string* since they're broken for `unicode` string literals. In Python 2 we need `f = u'\\\\?\\C:\\Users\\...'`. – Eryk Sun Jul 08 '19 at 16:57
  • In that case my other suggestion is not to use Python 2 ;-) – Tom Dalton Jul 09 '19 at 11:24

0 Answers0