python获取当前系统的桌面的路径的四种方法 | 您所在的位置:网站首页 › mac桌面文件路径是什么意思呀怎么删除 › python获取当前系统的桌面的路径的四种方法 |
原先以为获取这个应该不难,以为会像os.getcwd()一样简单。 结果百度呀谷歌呀,用中文搜英文搜,当然有搜到一个英文的,我放在第三个。 前两个方法是通过注册表来获取桌面路径。推荐使用第一个,因为不需要安装额外的扩展:) 一,用内置的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')
|
CopyRight 2018-2019 实验室设备网 版权所有 |