python下载路径在哪里找_python-查找用户的“下载”文件夹

在Python中,正确定位Windows文件夹有些麻烦。根据涵盖微软开发技术(如this one)的答案,应该使用Vista Known Folder API获得它们。这个API没有被Python标准库包装(尽管有an issue from 2008请求它),但是可以使用ctypes模块访问它。

修改上述答案以使用文件夹id进行下载shown here,并将其与现有的Unix代码相结合,应该会产生如下代码: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检索已知文件夹的更完整的模块是available on github。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值