Long paths in Python on Windows

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. Now I read mainly about two solutions:

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

This is good, but I cannot use it, since I need to use paths in the path

shutil.rmtree(short_path) 

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

  1. Prepare "\\\\?\\" for the path

I was not able to do this job. Attempting to do something this way always results in a WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: <path here> error WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: <path here>

So my question is: how to make the second option work? I emphasize that I need to use it in the same way as in the example in option No. 1.

OR

Is there another way?

EDIT: I need a solution to work in Python 2.7

EDIT2: question Support for a long Python file name, corrupted on Windows , gives an answer with a "magic prefix", and I stated that I know this in this question. What I do not know is HOW to use. I tried to add this path to the path, but it just failed, as I wrote above.

+8
python windows
source share
1 answer

Well, it seems that, as always, I found the answer to what listened to me for a week twenty minutes after I seriously asked someone about it.

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

  • A path can contain only a backslash, without a slash.
  • If I want to do something like a list in a directory, I need to end the path with a backslash, otherwise Python will add /*.* to it, which is a slash, which is bad.

Hope someone finds this helpful.

+8
source share

All Articles