1 Overview
- For some reasons, it is necessary to use the Windows desktop path of different users, so the path cannot be fixed. You can use the following methods to obtain it.
# Reference: Desktop address of Administrator accountC:\Users\Administrator\Desktop
2 Methods
2.1 Method 1: Use the os module
import os def get_desktop_path(): return (("~"), 'Desktop') desktop_path = get_desktop_path() print(desktop_path) # C:\Users\Administrator\Desktop
2.2 Method 2: Use winreg module
import winreg def get_desktop(): key = (winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders') return (key, "Desktop")[0] print(get_desktop()) # C:\Users\Administrator\Desktop
2.3 Method 3: Use the path module
from pathlib import Path def get_desktop_path(): return () / 'Desktop' print(get_desktop_path()) # C:\Users\Administrator\Desktop
This is the end of this article about three ways to obtain Windows desktop paths in Python. For more related contents of Python to obtain Windows desktop paths, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!