首页 > 解决方案 > Python - 远程启动项目时获取当前用户文件夹

问题描述

我有一个从 Jenkins 服务器启动的程序。

我使用 pyinstaller 创建了一个 exe 并将其安装在两台计算机上。然后詹金斯给他们两个打电话。

起初我使用os.path.expanduser('~')来获取用户路径,但它返回了

"C:\Windows\System32\config\systemprofile"

在我尝试使用os.environ['USERPROFILE']获取用户名之后 仍然得到:

"C:\Windows\System32\config\systemprofile"

最后我找到了一个不需要 os 模块的不同解决方案并尝试了:

import ctypes.wintypes
CSIDL_PERSONAL = 5       # My Documents
SHGFP_TYPE_CURRENT = 0   # Get current, not default value

buf= ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
ctypes.windll.shell32.SHGetFolderPathW(None, CSIDL_PERSONAL, None, 
SHGFP_TYPE_CURRENT, buf)

print(buf.value)

这在我的日志值中没有任何价值。

如果我在本地运行 exe,所有值都会正常返回。我运行的批处理命令是

start /wait C:\JenkinsResources\MarinaMain.exe

所以我画了一个空白,当我远程调用它时,我如何让这个程序找到它所在计算机的用户文件夹。

标签: pythonjenkinsremote-access

解决方案


所以为了让它工作,我首先下载了 Windows Power Shell 插件。重启后我使用了命令:

$PCAndUserName = Get-WMIObject -class Win32_ComputerSystem | select username

$UserName = [string]$PCAndUserName
$UserName = $UserName.split("\\")[-1]
$UserName = $UserName -replace ".{1}$"
$UserName

这给了我计算机上当前用户的名称,无论我是否使用 Jenkins 远程启动。


推荐阅读