[Python] Long paths in Python on Windows

Discussão em 'Python' iniciado por Stack, Abril 30, 2025.

  1. Stack

    Stack Membro Participativo

    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:\tools\Eclipse) 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.

    Continue reading...

Compartilhe esta Página