Python - 远程启动项目时获取当前用户文件夹

Python - Get current user folder when launching project remotely

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

我使用 pyinstaller 创建了一个 exe,并将其安装在两台计算机上。然后 Jenkins 称呼他们两个。

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

"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,所有值return恢复正常。 批处理命令 i 运行 是

start /wait C:\JenkinsResources\MarinaMain.exe

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

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

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

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

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