python現在のシステムデスクトップパスの取得

1153 ワード

一、内蔵winreg(推奨)
import winreg
def get_desktop():
    key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,\
                          r'Software\Microsoft\Windows\CurrentVersion\Explorer\ShellFolders',)
    return winreg.QueryValueEx(key, "Desktop")[0]

二、win 32拡張が必要
import win32api,win32con
def get_desktop():
    key =win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,\
                              r'Software\Microsoft\Windows\CurrentVersion\Explorer\ShellFolders',\
                             0,win32con.KEY_READ)
    return win32api.RegQueryValueEx(key,'Desktop')[0]

三、win 32拡張が必要
from win32com.shell import shell, shellcon
def GetDesktopPath():
    ilist =shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)
    return shell.SHGetPathFromIDList(ilist)

四、内蔵OS
import os
def GetDesktopPath():
    return os.path.join(os.path.expanduser("~"), 'Desktop')

この方法は、ユーザーがデスクトップパスを変更すると無効になります.