5

I created a directory above the character limit adding "\\?\" before the directory, but I can't delete it using shutil.rmtree or list it using os.walk.

I get the following error with shutil.rmtree("folder"):

WindowsError: [Error 3] The system cannot find the path specified: 'folder\CAAAAAAAAAB2iMan9VH4-0fxO4JOiT43bz9XVbQUoCcdOJTk1WRcPA++\BwAAAAAAAACXEWzr-_xJujcfpbaeAa-zNMqou1c_EtOH1lGXEMaL8w++\CAAAAAAAAACq0GkU9kGYNVDcaXAZ78ut8FSHTvE45Ra69qN495R6Fw++\CgAAAAAAAAAsOJ6oX-y6iRcg2F3KB4HGi6kcWnU2QPO2CEKsJUA4-g++'

Is there a function I can use to remove that directory?

Thanks.

Rodrigo Taboada
  • 2,727
  • 4
  • 24
  • 27
Protoless
  • 51
  • 3
  • 4
    To list or remove a long path, you need to use the absolute path as a unicode string, e.g. `shutil.rmtree(ur"\\?\C:\some\long\path\folder")`. – Eryk Sun Feb 13 '15 at 02:37
  • possible duplicate of [How to deal with files with a name longer than 259 characters?](http://stackoverflow.com/questions/5188527/how-to-deal-with-files-with-a-name-longer-than-259-characters) – ivan_pozdeev Feb 13 '15 at 03:27

2 Answers2

1

As pointed out by eryksun and also with more detail from links on the page linked to by ivan_pozdeev, the solution is summarized below:

Given a path which is longer than 260 characters long, shutil.rmtree cannot remove the file.

Let's assume

directory = "some path that is more than 260 characters".

An attempt to shutil.rmtree(directory) will fail with a *** WindowsError: [Error 3] The system cannot find the path specified: "some path that is more than 260 characters" error. It is not clear that the file exists and that the path length is the real problem.

The solution is to prepend \\?\ to the path which I do for arbitrary paths thusly:

shutil.rmtree(ur'\\?\ '.strip()+ directory, onerror=onerror)

Note that even when using the r'' syntax, the string may not end with a slash, so I solve this by adding a space and then strip()-ing the string.

This link was where it all became clear to me: https://github.com/nerdvegas/rez/issues/436

xobes
  • 191
  • 1
  • 5
  • Can you explain what `ur'\\?\'` is supposed to do? – ErikusMaximus May 20 '19 at 14:35
  • @ErikusMaximus As xobes says, this is a special symbol combination in Windows saying that the following path and each of its parts may be longer than 256 symbols. – Dr_Zaszuś Apr 05 '22 at 15:27
  • @Dr_Zaszuś Is this documented somewhere in the official Python documentation? If so, can you provide a link? – ErikusMaximus Apr 05 '22 at 15:52
  • 1
    @ErikusMaximus It is documented, but it's not a Python property, but a Windows property. So the official documentation is https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation and you can read more about it in this SE post: https://stackoverflow.com/questions/1880321/why-does-the-260-character-path-length-limit-exist-in-windows – Dr_Zaszuś Apr 06 '22 at 18:06
0

I had the same problem and after prefixing '\\?\' also had not helped, for me it was wrong delimiter in path, a combination of both forward slash and backward slash can cause it. For me it got fixed when I replaced forward slash (/) with backward slash (\).