Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long paths in Python on Windows

Tags:

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.

like image 721
Jiří Kantor Avatar asked Apr 10 '15 09:04

Jiří Kantor


People also ask

Should I disable path length limit Windows 10 Python?

Disable the path limit length is recommended after Python setup is successful, because if python was installed in a directory with a path length greater than 260 characters, adding it to the path could fail.


2 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.

like image 65
Jiří Kantor Avatar answered Sep 21 '22 12:09

Jiří Kantor


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
like image 27
Viktor Tóth Avatar answered Sep 20 '22 12:09

Viktor Tóth