您提供的链接中的错误报告包含一个
解决脚本,该脚本直接从Win32 API检索相关的主目录信息。请注意保留HTML标记。
import ctypes
from ctypes import windll, wintypes
class GUID(ctypes.Structure):
_fields_ = [
('Data1', wintypes.DWORD),
('Data2', wintypes.WORD),
('Data3', wintypes.WORD),
('Data4', wintypes.BYTE * 8)
]
def __init__(self, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8):
"""Create a new GUID."""
self.Data1 = l
self.Data2 = w1
self.Data3 = w2
self.Data4[:] = (b1, b2, b3, b4, b5, b6, b7, b8)
def __repr__(self):
b1, b2, b3, b4, b5, b6, b7, b8 = self.Data4
return 'GUID(%x-%x-%x-%x%x%x%x%x%x%x%x)' % (
self.Data1, self.Data2, self.Data3, b1, b2, b3, b4, b5, b6, b7, b8)
CSIDL_PROFILE = 40
FOLDERID_Profile = GUID(0x5E6C858F, 0x0E22, 0x4760, 0x9A, 0xFE, 0xEA, 0x33, 0x17, 0xB6, 0x71, 0x73)
def expand_user():
get_folder_path = getattr(windll.shell32, 'SHGetKnownFolderPath', None)
if get_folder_path is not None:
ptr = ctypes.c_wchar_p()
get_folder_path(ctypes.byref(FOLDERID_Profile), 0, 0, ctypes.byref(ptr))
return ptr.value
else:
get_folder_path = getattr(windll.shell32, 'SHGetSpecialFolderPathW', None)
buf = ctypes.create_unicode_buffer(300)
get_folder_path(None, buf, CSIDL_PROFILE, False)
return buf.value
这个
expand_user()
函数仅返回当前用户的主目录。
os.path.expanduser("~")
在我使用的 Windows 7 64 位操作系统和 Python 2.7 64 位版本中可以正常工作。 - Saullo G. P. Castro