Python doesn't understand %userprofile%

Tim Golden mail at
Tue Jun 10 21:06:52 CEST 2008

drobinow at wrote:
> On Jun 10, 2:09 pm, Duncan Booth < at invalid.invalid> wrote:
>> bsag... at wrote:
>>> In xp when I try os.path.getmtime("%userprofile/dir/file%") Python
>>> bites back with "cannot find the path specified" Since my script has
>>> to run on machines where the username is unspecified I need a fix.
>>> Thanks in advance.
>>>>> os.path.expanduser("~/dir/file")
>> 'C:\\Documents and Settings\\Duncan/dir/file'
> "~" appears to look first at the HOME environment variable.
> That is not necessarily the same as "USERPROFILE". On my machine it is
> not.

There was quite a debate over that on python-dev earlier
this year. In short, it's not easy to identify exactly what
"~" means on a Windows box. The implementer of that patch
took the view that HOME comes first and then USERPROFILE,
finally HOMEDRIVE/SHARE/PATH. On my machine at work,
the latter should take precedence as they are set via
my domain logon. Other people's mileage may vary. In addition,
the code assumes that the home for any *other* user can
always be derived from the *current* user's home.
Which will not always be the case.

All that is essentially why the user-specific functions
exposed in win32profile rely on a logon token to operate.

(Bit of a rant, but just to highlight that things are
seldom what they seem).


More information about the Python-list mailing list