代码之家  ›  专栏  ›  技术社区  ›  Jean-Paul Calderone

什么是pwd.getpwnam(用户名).pw\u dir的Windows等价物?

  •  8
  • Jean-Paul Calderone  · 技术社区  · 14 年前

    getpwnam(3) posixapi,可用于通过用户名获取特定用户的主目录,以及确定用户名是否有效。 pwd.getpwnam 如果使用不存在的用户名调用,将引发异常。

    起初,似乎可以通过跨平台的方式通过 os.path.expanduser('~username') . 然而,在windowsxp上使用python2.6时,对于一个不存在的用户名来说,这并不会导致失败。此外,在WindowsXP上的Python2.5上,即使对于有效用户,它也似乎失败了。

    4 回复  |  直到 10 年前
        1
  •  4
  •   Nas Banov    14 年前

    阅读 2.6 documentation 表明 os.path.expanduser() 在Windows上损坏:

    HOMEPATH和HOMEDRIVE的组合 ~用户 是 通过剥离最后一个 上面导出的用户路径。

    什么 ? 这假设所有的用户家庭必须在同一个父目录下。不!

    这有点难挖掘,但这里有一个解决方案,可以按给定的名称查找本地用户:

    from win32security import LookupAccountName, ConvertSidToStringSid
    from _winreg import OpenKey, QueryValueEx, HKEY_LOCAL_MACHINE
    
    def getUserDir(userName):
        ssid = ConvertSidToStringSid(LookupAccountName(None, userName)[0])
        key = OpenKey(HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\\' + ssid)
        return QueryValueEx(key, 'ProfileImagePath')[0]
    
        2
  •  3
  •   Community CDub    7 年前

    曾经有一个很好的维基百科的基思布朗.Net开发人员指南的Windows安全。。。你仍然可以在谷歌“pluralsight keith.guidebook”的缓存中找到它

    案例1:如果您没有用户密码:

    对于本地帐户,您可以尝试阅读nasbanov已经建议的Windows注册表,SO或Internet上还有其他一些方法。

    我不知道新创建的用户的各种Windows版本的行为。。。那些从未执行过交互式会话登录的用户。。。它是否会自动创建他们的注册表、主文件夹和配置文件数据? 我在WindowsXP上做了一些测试,在创建本地帐户后,那些注册表项不存在。。。但在这种情况下,您可以尝试根据所有用户的注册表值猜测它。。。或者只是失败:)

    from win32com.shell import shell, shellcon
    # See microsoft references for further CSIDL constants
    # http://msdn.microsoft.com/en-us/library/bb762181(VS.85).aspx
    folder_name = shell.SHGetFolderPath(0, shellcon.CSIDL_PROFILE, 0, 0)
    

    阅读基思·布朗的文章 How To Get A Token For A User " .. 你可以寻找一些其他的方法来获得一个没有密码的用户令牌。。。

    案例2:如果您拥有用户密码:

    阅读MSDN我得到了一个印象,如果我有一个用户令牌,我可以通过调用下面的代码得到它的文件夹。。。但这对我不起作用(不知道为什么)

    token = win32security.LogonUser(
                username,
                None, # we uses UPN format for username
                password,
                win32security.LOGON32_LOGON_NETWORK,
                win32security.LOGON32_PROVIDER_DEFAULT,
                )
    folder_name = shell.SHGetFolderPath(0, shellcon.CSIDL_PROFILE, token, 0)
    

    token = win32security.LogonUser(
                username,
                None, # Here should be the domain ... or just go with default values
                password,
                win32security.LOGON32_LOGON_NETWORK,
                win32security.LOGON32_PROVIDER_DEFAULT,
                )
    win32security.ImpersonateLoggedOnUser(token)
    folder_name = shell.SHGetFolderPath(0, shellcon.CSIDL_PROFILE, 0, 0)
    win32security.RevertToSelf()
    

    这个问题有某种关联: How to find the real user home directory using python?

        3
  •  0
  •   akira    14 年前

    你可以去公园 win32api.GetUserName() (仅限当前用户)或 win32net.NetUserGetInfo()

      import win32net
    
      def userDir(username):
            return win32net.NetUserGetInfo(None, username, 1).get("home_dir")
    

    或者,可以展开环境变量 USERPROFILE 在窗口或 HOME

      def userDir():
          if os.platform.system() == 'Windows':
              return os.environ['USERPROFILE']
          elif os.platform.system() == 'Linux':
              return os.environ['HOME'] 
          else:
              return None
    
        4
  •  0
  •   Wayne Werner    14 年前

    这似乎只适用于当前用户,但在我的(winxp)计算机上, os.path.expanduser('~') 返回我的主目录。