Get the desktop path in Python

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

Get the desktop path in Python
Get the desktop path in Python
Get the script path in Python
Get the host name in Python
Get the EDINET code list in Python
Get date in Python
[Python] Get the files in a folder with Python
Get the weather in Osaka via WebAPI (python)
Get the X Window System window title in Python
How to get the files in the [Python] folder
Get YouTube Comments in Python
Get last month in python
Download the file in Python
Find the difference in Python
Naturally sort Path in Python
Get Terminal size in Python
[Python] Get the previous month
Explicitly get EOF in python
Get Evernote notes in Python
Maya | Get the workspace path
Get Japanese synonyms in Python
How to get the variable name itself in python
How to get the number of digits in Python
[Python] Get the official file path of the shortcut file (.lnk)
[Python] Get the numbers in the graph image with OCR
[python] Get the list of classes defined in the module
Get the result in dict format with Python psycopg2
Get the size (number of elements) of UnionFind in Python
Get the value selected in Selenium Python VBA pull-down
Get the URL of the HTTP redirect destination in Python
Getting the arXiv API in Python
Get your heart rate from the fitbit API in Python!
Python in the browser: Brython's recommendation
Get data from Quandl in Python
Save the binary file in Python
Hit the Sesami API in Python
Calculate free-space path loss in Python
Get the MIME type in Python and determine the file format
Get the weather with Python requests
Get the weather with Python requests 2
In the python command python points to python3.8
Implement the Singleton pattern in Python
Get the number of specific elements in a python list
How to get the Python version
Get the value while specifying the default value from dict in Python
Get, post communication memo in Python
How to get the last (last) value in a list in Python
Hit the web API in Python
Automatically get the port where Arduino is stuck in Python
Get the current date and time in Python, considering the time difference
I wrote the queue in Python
Calculate the previous month in Python
[Python] Get the script execution directory with an absolute path
Examine the object's class in python
Project Euler # 15 "Lattice Path" in Python
Access the Twitter API in Python
Get the index of each element of the confusion matrix in Python
Python Note: Get the current month
The first step in Python Matplotlib
Get started with Python in Blender
Get the file path using Pathlib