Get the desktop path in Python

I was addicted to it unexpectedly, so I made a note.

Since the desktop path varies depending on the PC and user, describe how to obtain it for general purposes.

environment

Python 2.7 Windows 7/8

script

#coding:cp932

import os

desktop_path = os.getenv("HOMEDRIVE") + os.getenv("HOMEPATH") + "\\Desktop"

print desktop_path

Execution result:

python


C:\Users\foo\Desktop

Description

You can get environment variables with os.getenv ("???").

[Main environment variables (external link)](http://win.just4fun.biz/%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E3%83% 97% E3% 83% AD% E3% 83% B3% E3% 83% 97% E3% 83% 88 / Windows% E3% 81% AE% E4% B8% BB% E8% A6% 81% E3% 81% AA% E7% 92% B0% E5% A2% 83% E5% A4% 89% E6% 95% B0% E4% B8% 80% E8% A6% A7% E3% 81% A8% E6% 84% 8F% E5% 91% B3.html)

Since there is no environment variable that stores the desktop path itself, the above script creates the desktop path with a combination of environment variables.

bonus

I will also get my documents, pictures, etc.

#coding:cp932

import os

desktop_path = os.getenv("HOMEDRIVE") + os.getenv("HOMEPATH") + "\\Desktop"
mydocument_path = os.getenv("HOMEDRIVE") + os.getenv("HOMEPATH") + "\\Documents"
mypicrure_path = os.getenv("HOMEDRIVE") + os.getenv("HOMEPATH") + "\\Pictures"
myvideo_path = os.getenv("HOMEDRIVE") + os.getenv("HOMEPATH") + "\\Videos"
mymusic_path = os.getenv("HOMEDRIVE") + os.getenv("HOMEPATH") + "\\Music"

if os.path.exists(desktop_path):
    print "desktop:" + desktop_path
if os.path.exists(mydocument_path):
    print "document:" + mydocument_path
if os.path.exists(mypicrure_path):
    print "Picture:" + mypicrure_path
if os.path.exists(myvideo_path):
    print "video:" + myvideo_path
if os.path.exists(mymusic_path):
    print "Music:" + mymusic_path

Execution result:

python


Desktop: C:\Users\foo\Desktop
Document: C:\Users\foo\Documents
Picture: C:\Users\foo\Pictures
Video: C:\Users\foo\Videos
Music: C:\Users\foo\Music

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 caller of a function 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 numbers in the graph image with OCR
Get the value selected in Selenium Python VBA pull-down
Get the URL of the HTTP redirect destination in Python
Get Leap Motion data in Python.
Getting the arXiv API in Python
File / folder path manipulation 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
I wrote the stack in Python
Get the query string (query string) in Django