当前位置: 代码网 > it编程>前端脚本>Python > Python获取Windows桌面路径的三种方法

Python获取Windows桌面路径的三种方法

2024年12月02日 Python 我要评论
1 概述因为某些原因,需要使用不同用户的 windows 桌面路径,故无法对路径进行固定,可使用下列方法进行获取# 参考:administrator 账号的桌面地址c:\users\administr

1 概述

  • 因为某些原因,需要使用不同用户的 windows 桌面路径,故无法对路径进行固定,可使用下列方法进行获取
# 参考:administrator 账号的桌面地址
c:\users\administrator\desktop

2 方法

2.1 方法1:使用 os 模块

import os


def get_desktop_path():
    return os.path.join(os.path.expanduser("~"), 'desktop')


desktop_path = get_desktop_path()
print(desktop_path)
# c:\users\administrator\desktop

2.2 方法2:使用 winreg 模块

import winreg


def get_desktop():
    key = winreg.openkey(winreg.hkey_current_user, r'software\microsoft\windows\currentversion\explorer\shell folders')
    return winreg.queryvalueex(key, "desktop")[0]


print(get_desktop())
# c:\users\administrator\desktop

2.3 方法3:使用 path 模块

from pathlib import path


def get_desktop_path():
    return path.home() / 'desktop'


print(get_desktop_path())
# c:\users\administrator\desktop

到此这篇关于python获取windows桌面路径的三种方法的文章就介绍到这了,更多相关python获取windows桌面路径内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com