Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python2.7 ~ tilde not recognized in path as home directory macOs [duplicate]

At some point the tilde symbol ~ was no longer recognized as my home directory, only in Python. ~ still works with in terminal, so I'm not sure what happened but any insight on how to fix it you will save me some typing thanks!

On macOs Mojave

import os
tilde = '~'
print(os.path.exists(tilde))
os.system("if test -d ~; then echo 'exists'; fi")

OUTPUT:

False
exists
like image 416
buzz11 Avatar asked Oct 20 '25 15:10

buzz11


1 Answers

The ~ is interpreted by the OS, not Python. The way to use it from Python script is:

from os.path import expanduser
home = expanduser("~")

now home will have the path denoted by ~

like image 160
AbtPst Avatar answered Oct 23 '25 05:10

AbtPst



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!