python - Finding the user's "Downloads" folder

Viewed 47994
5 Answers
from pathlib import Path
downloads_path = str(Path.home() / "Downloads")

This fairly simple solution (expanded from this reddit post) worked for me

import os

def get_download_path():
    """Returns the default downloads path for linux or windows"""
    if os.name == 'nt':
        import winreg
        sub_key = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
        downloads_guid = '{374DE290-123F-4565-9164-39C4925E467B}'
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, sub_key) as key:
            location = winreg.QueryValueEx(key, downloads_guid)[0]
        return location
    else:
        return os.path.join(os.path.expanduser('~'), 'downloads')
  • The GUID can be obtained from Microsoft's KNOWNFOLDERID docs
  • This can be expanded to work more generically other directories

For python3+ mac or linux

from pathlib import Path
path_to_download_folder = str(os.path.join(Path.home(), "Downloads"))

Some linux distributions localize the name of the Downloads folder. E.g. after changing my locale to zh_TW, the Downloads folder became /home/user/下載. The correct way on linux distributions (using xdg-utils from freedesktop.org) is to call xdg-user-dir:

import subprocess
# Copy windows part from other answers here
try:
    folder = subprocess.run(["xdg-user-dir", "DOWNLOAD"],
                            capture_output=True, text=True).stdout.strip("\n")
except FileNotFoundError:  # if the command is missing
    import os.path
    folder = os.path.expanduser("~/Downloads")  # fallback

Note that the use of capture_output requires Python ≥3.7. If you already use GLib or don't mind adding more dependencies, see also these approaches using packages.

Related