Long paths in Python on Windows

Question:

I have a problem when programming in Python running under Windows. I need to work with file paths, that are longer than 256 or whatsathelimit characters.
Now, I’ve read basically about two solutions:

  1. Use GetShortPathName from kernel32.dll and access the file in this way.

That is nice, but I cannot use it, since I need to use the paths in a way

shutil.rmtree(short_path)

where the short_path is a really short path (something like D:toolsEclipse) and the long paths appear in the directory itself (damn Eclipse plugins).

  1. Prepend "\\?\" to the path

I haven’t managed to make this work in any way. The attempt to do anything this way always result in error WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: <path here>

So my question is: How do I make the 2nd option work? I stress that I need to use it the same way as in the example in option #1.

OR

Is there any other way?

EDIT: I need the solution to work in Python 2.7

EDIT2: The question Python long filename support broken in Windows does give the answer with the ‘magic prefix’ and I stated that I know it in this question. The thing I do not know is HOW do I use it. I’ve tried to prepend that to the path but it just failed, as I’ve written above.

Asked By: Jiří Kantor

||

Answers:

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.

Answered By: Jiří Kantor

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

  1. For python < 3: 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
Answered By: Viktor Tóth

py 3.8.2

# Fix long path access:
import ntpath
ntpath.realpath = ntpath.abspath
# Fix long path access.

In my case, this solved the problem of running a script from a long path.
(https://developers.google.com/drive/api/v3/quickstart/python)
But this is not a universal fix.
It looks like the ntpath.realpath implementation has problems. This code replaced it with a dummy.

Answered By: uDev

it works for me

import os
str1=r"C:Usersmanualdemodfadsfljdskfjslkdsjfklajinner-2djfklsdfjsdklfjinner3fadsfksdfjdklsfjksdgjlinner4dfhasdjfhsdjfskfklsjdkjfleioreirueewdsfksdmvanotherInnerfolder4aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa5qbbbbbbbbbbbccccccccccccccccccccccccssssssssssssssssstmp.txt"
print(len(str1)) #346

path = os.path.abspath(str1)

if path.startswith(u"\\"):
    path=u"\\?\UNC\"+path[2:]
else:
    path=u"\\?\"+path

with open(path,"r+") as f:
    print(f.readline())

if you get a long path(more then 258 char) issue in windows then try this .

Answered By: Sandeep Makwana
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.