python3获取windows桌面路经

方法1:(推荐,使用python内置库)

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]

方法2:win32扩展(需要安装第三方库)

import win32api,win32con
def get_desktop():
    key =win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',0,win32con.KEY_READ)
    return win32api.RegQueryValueEx(key,'Desktop')[0]

方法3.python内置的os库的path模块

import os
def GetDesktopPath():
    return os.path.join(os.path.expanduser("~"), 'Desktop')

参考:

https://blog.csdn.net/u013948858/article/details/75072873

你可能感兴趣的:(Python,python,windows桌面路经)