python - Finding the user's “Downloads” folder

后端 未结 4 1891
逝去的感伤
逝去的感伤 2020-12-05 14:53

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

I would like to achieve the same with the

4条回答
  •  青春惊慌失措
    2020-12-05 15:08

    Correctly locating Windows folders is somewhat of a chore in Python. According to answers covering Microsoft development technologies, such as this one, they should be obtained using the Vista Known Folder API. This API is not wrapped by the Python standard library (though there is an issue from 2008 requesting it), but one 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 result in code that looks 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 retrieving known folders from Python is available on github.

提交回复
热议问题