Python doesn't understand %userprofile%

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bsagert@gmail.com

    Python doesn't understand %userprofile%

    In xp when I try os.path.getmtim e("%userprofi le/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.
  • bsagert@gmail.com

    #2
    Re: Python doesn't understand %userprofile%

    On Jun 10, 8:56 am, bsag...@gmail.c om wrote:
    In xp when I try os.path.getmtim e("%userprofi le/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.getmtim e("%userprofile %/dir/file")

    Comment

    • Tim Golden

      #3
      Re: Python doesn't understand %userprofile%

      bsagert@gmail.c om wrote:
      In xp when I try os.path.getmtim e("%userprofi le/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:

      "%USERPROFI LE%/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.getmtim e (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

      Comment

      • Lie

        #4
        Re: Python doesn't understand %userprofile%

        On Jun 10, 11:11 pm, Tim Golden <m...@timgolden .me.ukwrote:
        bsag...@gmail.c om wrote:
        In xp when I try os.path.getmtim e("%userprofi le/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:
        >
        "%USERPROFI LE%/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.getmtim e (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

        Comment

        • Mike Driscoll

          #5
          Re: Python doesn't understand %userprofile%

          On Jun 10, 11:11 am, Tim Golden <m...@timgolden .me.ukwrote:
          bsag...@gmail.c om wrote:
          In xp when I try os.path.getmtim e("%userprofi le/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:
          >
          "%USERPROFI LE%/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.getmtim e (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.Deskto p() 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

          Comment

          • Duncan Booth

            #6
            Re: Python doesn't understand %userprofile%

            bsagert@gmail.c om wrote:
            In xp when I try os.path.getmtim e("%userprofi le/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.expan duser("~/dir/file")
            'C:\\Documents and Settings\\Dunca n/dir/file'

            Comment

            • drobinow@gmail.com

              #7
              Re: Python doesn't understand %userprofile%

              On Jun 10, 2:09 pm, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
              bsag...@gmail.c om wrote:
              In xp when I try os.path.getmtim e("%userprofi le/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.expand user("~/dir/file")
              >
              'C:\\Documents and Settings\\Dunca n/dir/file'
              "~" appears to look first at the HOME environment variable.
              That is not necessarily the same as "USERPROFIL E". On my machine it is
              not.

              Comment

              • Tim Golden

                #8
                Re: Python doesn't understand %userprofile%

                drobinow@gmail. com wrote:
                On Jun 10, 2:09 pm, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
                >bsag...@gmail. com wrote:
                >>In xp when I try os.path.getmtim e("%userprofi le/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.exp anduser("~/dir/file")
                >'C:\\Documen ts and Settings\\Dunca n/dir/file'
                >
                "~" appears to look first at the HOME environment variable.
                That is not necessarily the same as "USERPROFIL E". 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

                Comment

                • TheSaint

                  #9
                  Re: Python doesn't understand %userprofile%

                  On 00:11, mercoledì 11 giugno 2008 Tim Golden wrote:
                  "%USERPROFI LE%/dir/file".
                  os.environ('USE RPROFILE') 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

                  Comment

                  • castironpi

                    #10
                    Re: Python doesn't understand %userprofile%

                    On Jun 10, 6:51 pm, TheSaint <fc14301...@icq mail.comwrote:
                    On 00:11, mercoledì 11 giugno 2008 Tim Golden wrote:
                    >
                    "%USERPROFI LE%/dir/file".
                    >
                    os.environ('USE RPROFILE') 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.

                    Comment

                    • Tim Roberts

                      #11
                      Re: Python doesn't understand %userprofile%

                      bsagert@gmail.c om wrote:
                      >
                      >In xp when I try os.path.getmtim e("%userprofi le/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, timr@probo.com
                      Providenza & Boekelheide, Inc.

                      Comment

                      • Mike Driscoll

                        #12
                        Re: Python doesn't understand %userprofile%

                        On Jun 11, 1:58 am, Tim Roberts <t...@probo.com wrote:
                        bsag...@gmail.c om wrote:
                        >
                        In xp when I try os.path.getmtim e("%userprofi le/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

                        Comment

                        Working...