Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Python, can I be sure the expanded user call will be an absolute path if the path has "~" in it?

For example, is this expression always true?

path = '~/.my_app'
os.path.expanduser(path) == os.path.abspath(os.path.expanduser(path))
like image 313
Joe Avatar asked Jan 23 '14 18:01

Joe


1 Answers

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.

like image 115
Max Noel Avatar answered Oct 14 '22 06:10

Max Noel