How to get the desktop path in Python for both macOS and Windows.
There are many, but it's easy to use os.path.expanduser.
get_desktop.py
import os
desktop_dir = os.path.expanduser('~/Desktop')
print(desktop_dir)
It is also a good idea to get it from an environment variable, but in that case, OS judgment is required.
get_desktop2.py
import os
if os.name == 'nt':
home = os.getenv('USERPROFILE')
else:
home = os.getenv('HOME')
desktop_dir = os.path.join(home, 'Desktop')
print(desktop_dir)
That was a brief introduction.
Recommended Posts