Windows 10 Environment Variables

In Windows 10, the first 5 letters of your email are used to create a local folder on the system drive.

In my case this is C:\Users\Phili

This will be different in your case, meaning if you copy and paste the file location given it won't find anything on your computer.

There are a number of environment variables in Windows 10 which can be used to access each user location. They can be copied and pasted into the address bar:

For example %UserProfile%

Which in my case takes me to C:\Users\Phili and in your case will take you to your User Profile:

Local User Environment Variables

This can also be used to access commonly used libraries:

ShortcutLocation
%UserProfile%C:\Users\{username}
%UserProfile%\DocumentsC:\Users\{username}\Documents
%UserProfile%\PicturesC:\Users\{username}\Pictures
%UserProfile%\MusicC:\Users\{username}\Music
%UserProfile%\DesktopC:\Users\{username}\Desktop
%UserProfile%\DownloadsC:\Users\{username}\Downloads
%UserProfile%\VideosC:\Users\{username}\Videos

OneDrive Environment Variables

Note if the Documents, Pictures, Music and Desktop folders are fully integrated with OneDrive you can use:

ShortcutLocation
%OneDrive%C:\Users\{username}\OneDrive
%OneDrive%\DesktopC:\Users\{username}\OneDrive\Desktop
%OneDrive%\DocumentsC:\Users\{username}\OneDrive\Documents
%OneDrive%\PicturesC:\Users\{username}\OneDrive\Pictures
%OneDrive%\MusicC:\Users\{username}\OneDrive\Music

Public User Environment Variables

In a Windows 10 installation the permissions of each User Folder is locked to the specific user. A second user account on the computer generally cannot access the documents of the first user and vice-versa. To overcome this, there is the Public users folder. The public folder can optionally be shared across the network:

ShortcutLocation
%Public%C:\Users\Public
%Public%\DocumentsC:\Users\Public\Documents
%Public%\PicturesC:\Users\Public\Pictures
%Public%\MusicC:\Users\Public\Music
%Public%\DesktopC:\Users\Public\Desktop
%Public%\DownloadsC:\Users\Public\Downloads
%Public%\VideosC:\Users\Public\Videos

Other Common Folders Environment Variables

Other commonly accessed locations by programs are the Roaming AppData folder, the Local AppData folder, the Temporary folder, the ProgramFiles folder, the CommonProgramFiles folder, the ProgramData Folder and the Start Menu shortcuts:

ShortcutLocation
%AppData%C:\Users\{username}\AppData\Roaming
%LocalAppData%C:\Users\{username}\AppData\Local
%Temp%C:\Users\{username}\AppData\Local\Temp
%Tmp%C:\Users\{username}\AppData\Local\Temp
%ProgramFiles%C:\Program Files
%ProgramFiles(x86)%C:\Program Files (x86)
%CommonProgramFiles% C:\Program Files\Common Files
%CommonProgramFiles(x86)%C:\Program Files (x86)\Common Files
%ProgramData% C:\ProgramData
%AllUsersProfile%C:\ProgramData
%AppData%\Microsoft\Windows\Start Menu\Programs\StartupC:\Users\{username}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
%HomeDrive%C:\
%SystemDrive% C:\
%SystemRoot%C:\Windows

Creating a Custom Environment Variable

Right click the Start Button and select System:

Scroll down and select Advanced System Settings:

Select Environment Variables:

Select New:

Create a Variable Name (without denoting the %) and specify a Variable Name (the Location of the folder). Select OK and OK on the Environment Variables Window.

You can now use your new Environment Variable:

This will take you to the folder specified:

In Python the environment variables can be accessed by use of the os library. The environ attribute acts as a dictionary and the key is the Environment Variable. For example in the case of %UserProfile%

import os
os.environ['UserProfile']

The User Profile location is returned as a strong. Recall that \ is denotes a special character is to be displayed as part of a string in Python and \\ means to display a single \

This difference can be seen when the print statement is used.

To get the documents folder string concatenation can be used:

os.environ['UserProfile']+r'\Documents'

Recall that r before a string denotes a relative string. When this is used one can type in the folder location as \Documents opposed to \\Documents. The \ should be used before the folder.

If a file is included the \ should be included by the file name and extension. For example:

folder=os.environ['UserProfile']+r'\Documents'
excelfile=folder+'\Book1.xlsx'