Skip to content

Commit 5ea07ed

Browse files
committed
fix: 优化获取用户名的逻辑,增加权限和环境错误处理
1 parent b0a6e36 commit 5ea07ed

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

systemInfo/dev/main.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,15 @@ def main():
149149
print(f"操作系统: {platform.system()} {platform.release()}")
150150
print(f"主机名: {platform.node()}")
151151
try:
152+
# 尝试用 os.getlogin() 获取用户名
152153
print(f"用户名: {os.getlogin()}")
153-
except PermissionError:
154-
print(f"用户名: 无权限")
154+
except (PermissionError, OSError):
155+
# 捕获权限错误和终端环境错误,使用替代方案
156+
try:
157+
# 备选方案:通过环境变量获取用户名
158+
print(f"用户名: {os.environ.get('USER', os.environ.get('USERNAME', '未知'))}")
159+
except:
160+
print(f"用户名: 无法获取")
155161
print(f"架构: {platform.machine()}")
156162
print(f"平台: {platform.platform()}")
157163
boot_time = get_boot_time()

0 commit comments

Comments
 (0)