Python long filename support broken in Windows Python long filename support broken in Windows windows windows

Python long filename support broken in Windows


In order to use the \\?\ prefix (as already proposed), you also need to make sure you use Unicode strings as filenames, not regular (byte) strings.


For anyone else looking for solution here:

  1. You need to add prefix \\?\ as already stated, and make sure string is unicode;
  2. If you are using shutil, especially something like shutil.rmtree with onerror method, you'll need to modify it too to add prefix as it gets stripped somewhere on the way.

You'll have to write something like:

def remove_dir(directory):    long_directory = '\\\\?\\' + directory    shutil.rmtree(long_directory, onerror=remove_readonly)def remove_readonly(func, path, excinfo):    long_path = path    if os.sep == '\\' and '\\\\?\\' not in long_path:        long_path = '\\\\?\\' + long_path    os.chmod(long_path, stat.S_IWRITE)    func(long_path)

This is an example for Python 3.x so all strings are unicode.