Skip to content

Instantly share code, notes, and snippets.

@marcelomgarcia
Created August 2, 2023 13:15
Show Gist options
  • Save marcelomgarcia/9f6add4cf8cd76773f0dfe913eb5e18d to your computer and use it in GitHub Desktop.
Save marcelomgarcia/9f6add4cf8cd76773f0dfe913eb5e18d to your computer and use it in GitHub Desktop.
Python Windows Long Path Problems

Problems with Windows Long Path in Python

It seems there is a limit in Windows API paths, that is causing the Digital Preservation script to fail with the message of "file not found"

>>> source_dir = Path(r"G:\GSDC High Res Photos\2023-GSDC\2023-05-31-Day 2\2023-05-31-1030-1200-Using reconciliation and migration to drive the development of more sustainable and equitable societies")
>>> dest_dir = Path(r"E:\KAUST_ARCHIVES\009_COMMUNICATIONS_AND_PUBLIC_RELATIONS\001_GLOBAL_BRANDING_AND_COMMUNICATIONS\009_001_0018\mg_test")
>>> copytree(source_dir, dest_dir)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\shutil.py", line 559, in copytree
    return _copytree(entries=entries, src=src, dst=dst, symlinks=symlinks,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\shutil.py", line 513, in _copytree
    raise Error(errors)
shutil.Error: [('G:\\GSDC High Res Photos\\2023-GSDC\\2023-05-31-Day 2\\2023-05-31-1030-1200-Using reconciliation and migration to drive the development of more sustainable and equitable societies\\2023-05-31-1030-1200-Using reconciliation and migration to drive the development of more sustainable and equitable societies-001.JPG', 'E:\\KAUST_ARCHIVES\\009_COMMUNICATIONS_AND_PUBLIC_RELATIONS\\001_GLOBAL_BRANDING_AND_COMMUNICATIONS\\009_001_0018\\mg_test\\2023-05-31-1030-1200-Using reconciliation and migration to drive the development of more sustainable and equitable societies-001.JPG', "[Errno 2] No such file or directory

The path of the files in source_dir are bigger than the 260 characters limit:

>>> for child in source_dir.iterdir():
...     print(len(str(child)))
...
308
308
(...)

To enable long paths in Windows API it's necessary to change a key in Windows Registry

Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled

Currently this is not enabled in FRED

Key Name:          HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem
Class Name:        <NO CLASS>
Last Write Time:   4/18/2021 - 3:44 AM
(...)
Value 2
  Name:            LongPathsEnabled
  Type:            REG_DWORD
  Data:            0 <------ Not enabled

To fix the problem we will need to enable the LongPathsEnabled and reboot FRED.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment