python 3 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:win 32拡張(サードパーティライブラリのインストールが必要)
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