python獲取當前系統桌面路徑


一、用內置的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]

二、需要win32擴展

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]

三、需要win32擴展

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')

該方法在用戶改變了桌面路徑后,會失效  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM