The Python Oracle

Is os.path.expanduser("~/x") equivalent to os.path.abspath(os.path.expanduser("~/x"))?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Meadow

--

Chapters
00:00 Question
00:30 Accepted answer (Score 6)
01:10 Thank you

--

Full question
https://stackoverflow.com/questions/2131...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #path #crossplatform #absolutepath

#avk47



ACCEPTED ANSWER

Score 6


It depends on what your $HOME points to. On most properly set-up systems (every mainstream Linux distro, OSX and Windows) it'll point to an absolute path, e.g. /home/user or C:/Users/User. But if it's unset, improperly set or even changed manually (export HOME=.), expanduser may result in a relative path, in which case abspath will further change it.

But for most intents and purposes, you can assume that yes, both expressions are equivalent.