Skip to content

Instantly share code, notes, and snippets.

@chemacortes
Last active November 14, 2022 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chemacortes/96976c7f2adc1ce9489ecf0b4328baff to your computer and use it in GitHub Desktop.
Save chemacortes/96976c7f2adc1ce9489ecf0b4328baff to your computer and use it in GitHub Desktop.
Clase LongPath para usar rutas largas en windows

Long Paths

En windows, las rutas están limitadas a un máximo de 260 caracteres (MAX_PATH). Python soporta tamaños mayores, pero es necesario que se active en el sistema:

https://docs.python.org/3/using/windows.html#removing-the-max-path-limitation

Si no es posible asegurar esta ativación, se añade la siguiente clase que facilita el uso de rutas largas a través de la nomenclatura UNC:

from pathlib import WindowsPath
class LongPath(WindowsPath):
uncprefix = "\\\\?\\"
def __new__(cls, *pathsegments):
ins = super().__new__(cls, *pathsegments)
abspath = ins.expanduser().absolute()
match abspath.parts:
case (drive, *parts) if not drive.startswith(cls.uncprefix):
drive = cls.uncprefix + drive
return super().__new__(cls,drive,*parts)
case _:
return ins
def short_path(self) -> WindowsPath:
(drive, *parts) = self.parts
return WindowsPath(drive.removeprefix(self.uncprefix), *parts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment