python通过执行命令获取windows的用户名

darling / 2024-09-02 / 原文


import os


def get_current_user():
    # whoami命令返回的信息中,第一个"/"之后的内容就是用户名
    # 我们使用split("/")方法获取用户名
    whoami_output = os.popen("whoami").read()
    return whoami_output.split("\\")[1]


print(get_current_user())