Python - Searching the user's Downloads folder

I already found this question that suggests using os.path.expanduser(path) to get the user's home directory.

I would like to achieve the same with the Downloads folder. I know that this is possible in C # , but I am new to Python and do not know if this is possible here, preferably regardless of the platform (Windows, Ubuntu).

I know that I could just do download_folder = os.path.expanduser("~")+"/Downloads/" , but (at least on Windows) you can change the default download folder .

+12
python directory folders special-folders
source share
3 answers

Correctly positioning Windows folders is quite a challenge in Python. According to answers regarding Microsoft development technologies such as this one , they must be obtained using the Vista API of known files . This API is not wrapped in the standard Python library (although there is a problem since 2008 requesting it), but you can use the ctypes module to access it anyway.

Adapting the above answer to use the folder id for downloads shown here , and combining it with your existing Unix code should make the code look like this:

 import os if os.name == 'nt': import ctypes from ctypes import windll, wintypes from uuid import UUID # ctypes GUID copied from MSDN sample code class GUID(ctypes.Structure): _fields_ = [ ("Data1", wintypes.DWORD), ("Data2", wintypes.WORD), ("Data3", wintypes.WORD), ("Data4", wintypes.BYTE * 8) ] def __init__(self, uuidstr): uuid = UUID(uuidstr) ctypes.Structure.__init__(self) self.Data1, self.Data2, self.Data3, \ self.Data4[0], self.Data4[1], rest = uuid.fields for i in range(2, 8): self.Data4[i] = rest>>(8-i-1)*8 & 0xff SHGetKnownFolderPath = windll.shell32.SHGetKnownFolderPath SHGetKnownFolderPath.argtypes = [ ctypes.POINTER(GUID), wintypes.DWORD, wintypes.HANDLE, ctypes.POINTER(ctypes.c_wchar_p) ] def _get_known_folder_path(uuidstr): pathptr = ctypes.c_wchar_p() guid = GUID(uuidstr) if SHGetKnownFolderPath(ctypes.byref(guid), 0, 0, ctypes.byref(pathptr)): raise ctypes.WinError() return pathptr.value FOLDERID_Download = '{374DE290-123F-4565-9164-39C4925E467B}' def get_download_folder(): return _get_known_folder_path(FOLDERID_Download) else: def get_download_folder(): home = os.path.expanduser("~") return os.path.join(home, "Downloads") 

A more complete module for extracting famous folders from Python is available on github .

+6
source share

This pretty simple solution (extended 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 the Microsoft documentation KNOWNFOLDERID
  • This can be expanded to more general work with other directories.
+8
source share
 import os download_path='/'.join( os.getcwd().split('/')[:3] ) + '/Downloads' 
0
source share

All Articles