Re: Python doesn't understand %userprofile%
- From: Mike Driscoll <kyosohma@xxxxxxxxx>
- Date: Tue, 10 Jun 2008 10:22:10 -0700 (PDT)
On Jun 10, 11:11 am, Tim Golden <m...@xxxxxxxxxxxxxxx> wrote:
bsag...@xxxxxxxxx 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
.
- References:
- Python doesn't understand %userprofile%
- From: bsagert
- Re: Python doesn't understand %userprofile%
- From: Tim Golden
- Python doesn't understand %userprofile%
- Prev by Date: Re: problems with opening files due to file's path
- Next by Date: Re: proposal: give delattr ability to ignore missing attribute
- Previous by thread: Re: Python doesn't understand %userprofile%
- Next by thread: Re: Python doesn't understand %userprofile%
- Index(es):
Relevant Pages
|