menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Programming News

>

Fabio Zadr...
source image

PlanetPython

1M

read

437

img
dot

Fabio Zadrozny: Using (or really misusing) Path.resolve() in Python

  • Python Path.resolve(), which resolves symlinks and substs on Windows could be causing bugs in certain cases.
  • If a user crafted a structure with symlink which isn't the canonical representation, resolve() will not find it.
  • In python docs, it is recommended to use Path.resolve() to walk up an arbitrary filesystem path.
  • However, it leads to incorrect parent resolutions. Instead, using os.path.normalize and os.path.abspath() will suffice.
  • Using os.path.normalize(os.path.abspath(...)) will eliminate instances of ‘..’ from the string and make it absolute.
  • A bug can occur when calls to absolute() or resolve() are made from two programs with different current working directories.
  • Path.resolve() can be useful in maping out an IDE cache where all representation of a file are considered the same.
  • Ideally, it's best to use the inode of the file as reference, since bugs can occur which result in the same file being opened under different names.
  • In case of executing an API call from another program, paths should already be absolute and normalized.
  • It is best practice to call Path(os.path.normalize(os.path.abspath(...))) on program boundaries.

Read Full Article

like

26 Likes

For uninterrupted reading, download the app