1

Still very new to Python. Within Windows using Python 3.7.

I would like to have a way of using Python to find windows user directories e.g. "Downloads" that have been moved to a secondary drive using the supported windows method.

As described by

https://www.windowscentral.com/how-move-default-user-folders-new-drive-windows-10

I prefer not to use Symlinks for this though it would bypass the issue.

Currently I use as

os.path.expanduser('~/Downloads')

This returns the default user folder say C:/Users/user/ followed by the folder mentioned C:/Users/user/downloads rather than linking to the user defined default folder for downloads in this case say D:/user/Downloads.

Could you please advise me on the right approach?

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
Ambrotos42
  • 13
  • 2
  • Have you tried using the direct path name? – Barb Sep 28 '19 at 16:56
  • Aren’t you supposed to use the `%NAMES%` for localization support anyway? – Davis Herring Sep 28 '19 at 19:09
  • I built a workaround that manually searches both "user folders" for windows and defaults to `os.path.expanduser()` for other OS's. I like coding reusable function "libraries" for reference so prefer a more flexible method. There appears to be a more complete method involving obtaining it from the registry using the GUID (KNOWNFOLDERID) of the folder using the winreg library. I just expected a simpler method to exist. I found it at [link](https://stackoverflow.com/questions/35851281/python-finding-the-users-downloads-folder#35851955) after rephrasing the question a few times. – Ambrotos42 Sep 28 '19 at 21:27
  • Regarding the `%NAMES%` comment by Davis somehow accessing `%HOMEPATH%` directly would provide a simpler solution but I do not know of any method to do so. – Ambrotos42 Sep 28 '19 at 21:27

1 Answers1

0

Windows stores these paths in environment variables. You should be able to access those in Python using e.g. os.getenv('HOMEPATH'). If it's not in a variable, you may be able to find it in the Windows registry. You can access that using the winreg module in the standard library (in Windows distributions only, of course).

gilch
  • 10,813
  • 1
  • 23
  • 28
  • `homepath = os.getenv('HOMEPATH') homepath = os.path.realpath(homepath+'/Downloads')` Produced the desired result. Thank you. – Ambrotos42 Sep 29 '19 at 06:48