desktop_path = os.path.join(os.environ.get('XDG_DESKTOP_DIR', os.path.expanduser('~/Desktop'))) print(desktop_path) 这里使用os.environ.get()来获取XDG_DESKTOP_DIR的值,如果该变量不存在,则使用os.path.expanduser('~/Desktop')作为默认路径。 六、总结与最佳实践 总结以上几种获取桌面路径的方法,...
Path.home()方法返回当前用户的主目录,然后通过/运算符拼接字符串得到桌面路径。 方法三:使用winreg模块(仅限Windows) python import winreg def get_desktop(): key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders') desktop = winreg.Query...
0,win32con.KEY_READ) returnwin32api.RegQueryValueEx(key,'Desktop')[0] 三,也需要win32扩展,原理不懂 fromwin32com.shellimportshell, shellcon defGetDesktopPath(): ilist=shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP) returnshell.SHGetPathFromIDList(ilist) 如果你相信我的话,就选择第一种...
desktop_path=os.path.expanduser('~/Desktop')print(desktop_path) 1. 2. 3. 4. 方法二:使用xdg模块 importxdg desktop_path=xdg.DesktopEntry.DesktopEntry.get('Desktop').get('Exec')print(desktop_path) 1. 2. 3. 4. 方法三:通过环境变量获取 desktop_path=os.getenv('XDG_DESKTOP_DIR')print(des...
答案:使用winreg模块:winreg模块是Python内置的一部分,无需额外安装。它可以用来访问Windows注册表,从而获取桌面路径。获取桌面路径的代码示例:“`pythonimport winreg def get_desktop_path: key = winreg.OpenKey desktop_path, regtype = winreg.QueryValueEx winreg.CloseKey ...
importosdef GetDesktopPath():returnos.path.join(os.path.expanduser("~"),'Desktop') AI代码助手复制代码 5.使用socket模块(不推荐) 当然这个方法,是获取当前pc的主机名,一般情况下,windows系统启用第一次开始时会设置一个电脑名,这个电脑名会出现在C盘下的用户目录下,比如我的电脑名是‘jayzhen',那么会有...
3、ionexplorershell folders', 0,win32con.key_read) return win32api.regqueryvalueex(key,'desktop')0 三,也需要win32扩展,原理不懂 from win32com.shell import shell, shellcondef getdesktoppath(): ilist = shell.shgetspecialfolderloion(0, shellcon.csidl_desktop) return shell.shgetpathfromidlist(ilis...
1 #该方法在用户改变了桌面路径后,会失效 2 import os 3 def GetDesktopPath(): 4 return os.path.join(os.path.expanduser("~"), 'Desktop')
("用户主目录:",user_home_os)print("桌面路径:",desktop_path_os)print("下载路径:",downloads_path_os)print("\n使用 pathlib 模块:")print("用户主目录:",user_home_pathlib)print("桌面路径:",desktop_path_pathlib)print("下载路径:",downloads_path_pathlib)if__name__=="__main__":get_user...
importwinregdefget_desktop_shortcuts():shortcuts=[]try:# 打开注册表key=winreg.OpenKey(winreg.HKEY_CURRENT_USER,r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")# 读取桌面路径desktop_path=winreg.QueryValueEx(key,"Desktop")[0]# 关闭注册表winreg.CloseKey(key)# 遍历桌面路径下...