Jiří Kantor
Jiří Kantor

Reputation: 752

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

Upvotes: 33

Views: 48686

Answers (6)

Zakria Askani
Zakria Askani

Reputation: 1

Just Follow my steps and long path will be enabled.

1.Open start menu and search for "Registry Editor"

2."Run as Administrator" and click "yes"

3.Export The Backup. Click the file option on the top left corner and Export the backup of the Registry Data in a the file of you choice.(Done)

4.Now Expend the "HKEY_LOCAL_MACHINE" Folder

5.Expend "System" Folder.

6.Expend "CurrentControlSet" Folder.

7.Expend "Control" Folder.

8.Expend "FileSystem" Folder.

9.You see a list of files in "FileSystem" Folder.

10.Find "LongPathEnabled" File and double click on it.

11.Change the data "Value Data" from 0 to 1 and click OK.

Now Your long Path Is Enabled. Congratulations

Upvotes: 0

user1698966
user1698966

Reputation: 13

This helped me out to systematically implement Viktor Tóth´s answer:

str1 = '../test/test/test/test/test/tesassat/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test\test_dataset.txt' #Relative path for testing
str1_unicode = str1.encode('unicode_escape').decode() # Step 1: Unicode
file_path = os.path.abspath(os.path.normpath(str1_unicode )) #Step 3 and 4: no forward slashes and absolute path
with open(file_path, "w") as f:
    print( "Just testing!")

Upvotes: 0

Viktor T&#243;th
Viktor T&#243;th

Reputation: 499

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

Upvotes: 18

Sandeep Makwana
Sandeep Makwana

Reputation: 43

it works for me

import os
str1=r"C:\Users\manual\demodfadsfljdskfjslkdsjfklaj\inner-2djfklsdfjsdklfj\inner3fadsfksdfjdklsfjksdgjl\inner4dfhasdjfhsdjfskfklsjdkjfleioreirueewdsfksdmv\anotherInnerfolder4aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\5qbbbbbbbbbbbccccccccccccccccccccccccsssssssssssssssss\tmp.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 .

Upvotes: 3

uDev
uDev

Reputation: 41

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.

Upvotes: 3

Jiř&#237; Kantor
Jiř&#237; Kantor

Reputation: 752

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.

Upvotes: 18

Related Questions