22

how to find if the file is a link file, and find the path of the target file (actual file pointed by the link file)

duhhunjonn
  • 44,855
  • 11
  • 28
  • 15

2 Answers2

33

os.path.islink (is it a link?) and os.path.realpath (get ultimate pointed to path, regardless of whether it's a link).

If os.path.islink is True, and you only want to follow the first link, use os.readlink.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
0

Use os.lstat(), then inspect the st_mode field.

unwind
  • 391,730
  • 64
  • 469
  • 606