Long paths in Python on Windows

Let me just simplify this for anyone looking for a straight answer:

  1. Path needs to be unicode, prepend string with u like u'C:\\path\\to\\file'
  2. Path needs to start with \\\\?\\ (which is escaped into \\?\) like u'\\\\?\\C:\\path\\to\\file'
  3. No forward slashes only backslashes: / --> \\
  4. It has to be an absolute path; it does not work for relative paths

Well it seems that, as always, I've found the answer to what's been bugging me for a week twenty minutes after I seriously ask somebody about it.

So I've found that I need to make sure two things are done correctly:

  1. The path can contain only backslashes, no forward slashes.
  2. If I want to do something like list a directory, I need to end the path with a backslash, otherwise Python will append /*.* to it, which is a forward slash, which is bad.

Hope at least someone will find this useful.

Tags:

Python

Windows