python - 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
所以我画了一个空白,当我远程调用它时,我如何让这个程序找到它所在计算机的用户文件夹。
解决方案
所以为了让它工作,我首先下载了 Windows Power Shell 插件。重启后我使用了命令:
$PCAndUserName = Get-WMIObject -class Win32_ComputerSystem | select username
$UserName = [string]$PCAndUserName
$UserName = $UserName.split("\\")[-1]
$UserName = $UserName -replace ".{1}$"
$UserName
这给了我计算机上当前用户的名称,无论我是否使用 Jenkins 远程启动。
推荐阅读
- flutter - Flutter android studio 导入包路径
- amazon-s3 - Camel S3:在调度程序关闭的情况下列出 S3 存储桶文件
- vba - Vba宏下标超出范围
- google-cloud-storage - Cloud Dataflow GlobalWindow 触发器被忽略
- unix - 使用非交互式 SSH 登录删除文件
- python - “这是 WebDriver 服务器的初始起始页” - 无法在使用 IE 11 的 Selenium/Python 程序中解析
- ionic-framework - 无法播放某些 Vimeo 视频。是否可以在 ionic 上设置 Referer 请求标头
- javascript - 当我在 React 中的类 Component 的开头声明一个变量时出错
- javascript - 使用酶进行浅渲染:实际输出与预期输出不匹配
- solr - Hybris 自定义方面排序提供程序不起作用