Re: Python doesn't understand %userprofile%



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
.



Relevant Pages

  • Re: Python doesnt understand %userprofile%
    ... the expansion of environment variables like USERPROFILE is done for you by the shell or the command prompt. ... what do you mean "run on machines where the username is ... can get quite complicated if users have roaming profiles and the like. ...
    (comp.lang.python)
  • Re: Python doesnt understand %userprofile%
    ... what do you mean "run on machines where the username is ... have a userprofile in any case: it might be the Default User ... can get quite complicated if users have roaming profiles and the like. ...
    (comp.lang.python)
  • inconsistant log on/log off lock up
    ... trouble logging on or logging off computers. ... aging systems to our current DCs. ... case with these machines. ... have ever been filled to capacity due to profiles. ...
    (microsoft.public.windowsxp.general)
  • Re: Howto replicate AS400 User Profiles
    ... |> | We have two physical machines here and user profiles currently ... |> | to replicate all user profiles to these too. ... |> With a lot more work, my team built a real time system, but issues ... | Does your real time option use exit points? ...
    (comp.sys.ibm.as400.misc)
  • Re: Different roaming profile path for different computers
    ... usual office machines. ... these users in ad to not have roaming profiles, in this way the users will ... say two groups of computers (by mean of AD ...
    (microsoft.public.windows.server.active_directory)