os.path.expanduser results in different directory?

67 views
Skip to first unread message

Panupat Chongstitwattana

unread,
May 18, 2012, 1:27:43 AM5/18/12
to python_in...@googlegroups.com
Hi.

I'm storing some local files location using the following command

LITEPATH = os.path.normpath(os.path.expanduser('~') + '\\dirName')

In Maya I would get , C:\Users\Me\Documents\dirName
But when running alone using python IDE, I get C:\Users\Me\dirName

What could be the cause of this and how I can force it to be identical?

Justin Israel

unread,
May 18, 2012, 9:30:43 AM5/18/12
to python_in...@googlegroups.com, python_in...@googlegroups.com
Take a look at the docs for os.path.expanduser:

There are a few environment variables that it will check to try and get a value to be joined to the user name. Maya could very well be changing those. Try printing out the values of them and seeing what they are. You may need to join it manually using a specific env variable. 

print os.getenv("HOME")
print os.getenv("USERPROFILE")

Linux has a pwd module that works regardless of env. 
Apparently there is a windows specific approach as well:

It just depends on if you are trying to make a cross platform compatible script? If so, you will have to check a couple cases for the current platform, and also not use "\\" for paths. Use "/", or just os.path.join to make sure paths work no matter which OS
Reply all
Reply to author
Forward
0 new messages