windows 您如何获得“我的文档”的确切路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3927259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:24:26  来源:igfitidea点击:

How do you get the exact path to "My Documents"?

pythonwindows

提问by Mark Ransom

In C++ it's not too hard to get the full pathname to the folder that the shell calls "My Documents" in Windows XP and Windows 7 and "Documents" in Vista; see Get path to My Documents

在 C++ 中,获取到 shell 在 Windows XP 和 Windows 7 中称为“我的文档”以及在 Vista 中称为“文档”的文件夹的完整路径名并不难;请参阅获取我的文档的路径

Is there a simple way to do this in Python?

有没有一种简单的方法可以在 Python 中做到这一点?

回答by Josh

You could use the ctypes module to get the "My Documents" directory:

您可以使用 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!")

Source: http://bugs.python.org/issue1763#msg62242

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