By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

Python doesn't understand %userprofile%

P: n/a
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.
Jun 27 '08 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On Jun 10, 8:56 am, bsag...@gmail.com 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.
oops that should be os.path.getmtime("%userprofile%/dir/file")
Jun 27 '08 #2

P: n/a
bs*****@gmail.com 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.
Well I can see a few problems here.

First is that putting percent signs around the whole path is
never going to work anyway. You want something like:

"%USERPROFILE%/dir/file".

Secondly, the expansion of environment variables like
USERPROFILE is done for you by the shell or the command
prompt. You have to do it for yourself if you're opening your
own files. You want something like:

import os
print os.path.getmtime (os.path.join (os.environ['USERPROFILE'], "ntuser.ini"))

But finally, what do you mean "run on machines where the username is
unspecified"? If you mean: where no user is logged in, then you won't
have a (meaningful) userprofile in any case: it might be the Default User
profile; I'm not sure. But is that what you want?

You *can* use the functions in the win32profile module of the pywin32
packages to find out various things about profiles directories, but things
can get quite complicated if users have roaming profiles and the like.

TJG
Jun 27 '08 #3

P: n/a
Lie
On Jun 10, 11:11*pm, Tim Golden <m...@timgolden.me.ukwrote:
bsag...@gmail.com 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.

Well I can see a few problems here.

First is that putting percent signs around the whole path is
never going to work anyway. You want something like:

"%USERPROFILE%/dir/file".

Secondly, the expansion of environment variables like
USERPROFILE is done for you by the shell or the command
prompt. You have to do it for yourself if you're opening your
own files. You want something like:

import os
print os.path.getmtime (os.path.join (os.environ['USERPROFILE'], "ntuser.ini"))

But finally, what do you mean "run on machines where the username is
unspecified"? If you mean: where no user is logged in, then you won't
have a (meaningful) userprofile in any case: it might be the Default User
profile; I'm not sure. But is that what you want?
I think what he meant is where he doesn't know in advance the name of
the user name.
You *can* use the functions in the win32profile module of the pywin32
packages to find out various things about profiles directories, but things
can get quite complicated if users have roaming profiles and the like.

TJG
Jun 27 '08 #4

P: n/a
On Jun 10, 11:11*am, Tim Golden <m...@timgolden.me.ukwrote:
bsag...@gmail.com 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.

Well I can see a few problems here.

First is that putting percent signs around the whole path is
never going to work anyway. You want something like:

"%USERPROFILE%/dir/file".

Secondly, the expansion of environment variables like
USERPROFILE is done for you by the shell or the command
prompt. You have to do it for yourself if you're opening your
own files. You want something like:

import os
print os.path.getmtime (os.path.join (os.environ['USERPROFILE'], "ntuser.ini"))

But finally, what do you mean "run on machines where the username is
unspecified"? If you mean: where no user is logged in, then you won't
have a (meaningful) userprofile in any case: it might be the Default User
profile; I'm not sure. But is that what you want?

You *can* use the functions in the win32profile module of the pywin32
packages to find out various things about profiles directories, but things
can get quite complicated if users have roaming profiles and the like.

TJG
Tim,

I'm surprised you didn't mention your excellent winshell utility. I
use it for this sort of issue all the time where I need to update
files on login and I don't know the user's name beforehand.

The winshell.Desktop() one has been a life saver and I think the OP
could probably use winshell for their problem. Or I may be completely
off my rocker.

Either way, here's the link: http://timgolden.me.uk/python/winshell.html

Mike
Jun 27 '08 #5

P: n/a
bs*****@gmail.com 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'
Jun 27 '08 #6

P: n/a
On Jun 10, 2:09 pm, Duncan Booth <duncan.bo...@invalid.invalidwrote:
bsag...@gmail.com 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.
Jun 27 '08 #7

P: n/a
dr******@gmail.com wrote:
On Jun 10, 2:09 pm, Duncan Booth <duncan.bo...@invalid.invalidwrote:
>bsag...@gmail.com 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).

TJG
Jun 27 '08 #8

P: n/a
On 00:11, mercoledì 11 giugno 2008 Tim Golden wrote:
"%USERPROFILE%/dir/file".
os.environ('USERPROFILE') should return an info regarding that environment
variable.
I guess that, not yet tried.
--
Mailsweeper Home : http://it.geocities.com/call_me_not_now/index.html
Jun 27 '08 #9

P: n/a
On Jun 10, 6:51*pm, TheSaint <fc14301...@icqmail.comwrote:
On 00:11, mercoledž 11 giugno 2008 Tim Golden wrote:
"%USERPROFILE%/dir/file".

os.environ('USERPROFILE') should return an info regarding that environment
variable.
I guess that, not yet tried.
--
Mailsweeper Home :http://it.geocities.com/call_me_not_now/index.html
I found:

from os import environ
r'%(HOMEPATH)s\My Documents\My Pictures\pycon.bmp'% environ

But 'os.join' is supposedly more correct.
Jun 27 '08 #10

P: n/a
bs*****@gmail.com 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.
For the record, the %PERCENT% syntax for looking up an environment variable
is just a feature of the XP command shell. It has no meaning to any other
part of Windows.

os.environ is the right answer.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #11

P: n/a
On Jun 11, 1:58*am, Tim Roberts <t...@probo.comwrote:
bsag...@gmail.com 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.

For the record, the %PERCENT% syntax for looking up an environment variable
is just a feature of the XP command shell. *It has no meaning to any other
part of Windows.

os.environ is the right answer.
--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.

You can use it at the Run command or in Explorer too. If I type
%username% in either place, it opens the following on my XP machine: C:
\Documents and Settings\Mike

Mike
Jun 27 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.