python - 查找用户的“下载";文件夹 [英] python - Finding the user's "Downloads" folder

查看:22
本文介绍了python - 查找用户的“下载";文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发现这个问题建议使用os.path.expanduser(path) 获取用户的主目录.

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

我想通过下载"文件夹实现相同的目标.我知道 这在 C# 中是可能的,但我是 Python 新手并且不不知道这在这里是否也可能,最好是独立于平台的(Windows、Ubuntu).

I would like to achieve the same with the "Downloads" folder. I know that this is possible in C#, yet I'm new to Python and don't know if this is possible here too, preferable platform-independent (Windows, Ubuntu).

我知道我可以做 download_folder = os.path.expanduser("~")+"/Downloads/",但 (至少在 Windows 中)可以更改默认下载文件夹.

I know that I just could do download_folder = os.path.expanduser("~")+"/Downloads/", yet (at least in Windows) it is possible to change the Default download folder.

推荐答案

在 Python 中正确定位 Windows 文件夹有点麻烦.根据涵盖 Microsoft 开发技术的答案,例如 这个,它们应该使用 Vista 已知文件夹 API.这个 API 没有被 Python 标准库封装(尽管有 一个 2008 年的问题 要求它),但是无论如何都可以使用 ctypes 模块来访问它.

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.

调整上述答案以使用文件夹 ID 进行下载 此处显示 并将其与您现有的 Unix 代码相结合应该会产生如下所示的代码:

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")

用于从 Python 检索已知文件夹的更完整模块是可在 github 上获得.

A more complete module for retrieving known folders from Python is available on github.

这篇关于python - 查找用户的“下载";文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆