如何获取“我的文档”的确切路径?

13 投票
1 回答
7698 浏览
提问于 2025-04-16 05:26

在C++中,获取Windows XP和Windows 7中被称为"My Documents"的文件夹的完整路径并不太难,而在Vista中则叫"Documents";你可以查看这个链接了解更多信息:获取我的文档路径

那么在Python中,有没有简单的方法来做到这一点呢?

1 个回答

15

你可以使用ctypes这个模块来获取“我的文档”目录:

import ctypes
from ctypes.wintypes import MAX_PATH

dll = ctypes.windll.shell32
buf = ctypes.create_unicode_buffer(MAX_PATH + 1)
if dll.SHGetSpecialFolderPathW(None, buf, 0x0005, False):
    print(buf.value)
else:
    print("Failure!")

来源:http://bugs.python.org/issue1763#msg62242

撰写回答