0

(It seems this would be a duplicate, but I cannot find. Please mark if you find it!)

I'm just curious, why does os.path.exists('~') return False? Clearly, my home folder does exist.

whoopscheckmate
  • 746
  • 8
  • 23
  • 5
    Because Python doesn't parse `~` as the home folder. Use `os.path.expanduser()` to convert it to an actual pathname. – Barmar Jan 18 '22 at 17:17
  • 1
    From the top of the documentation for the os.path module: `Unlike a unix shell, Python does not do any automatic path expansions. Functions such as expanduser() and expandvars() can be invoked explicitly when an application desires shell-like path expansion. (See also the glob module.)` – Karl Knechtel Jan 18 '22 at 17:20
  • @Barmar - so: `os.path.exists(os.path.expanduser('~'))` ? – RufusVS Jan 18 '22 at 17:23
  • RufusVS: Jeez, just ***try it*** yourself. – martineau Jan 18 '22 at 18:02

0 Answers0