Python, get windows special folders for currently logged-in user

后端 未结 5 992
终归单人心
终归单人心 2020-11-27 17:56

How can I get Windows special folders like My Documents, Desktop, etc. from my Python script? Do I need win32 extensions?

It must work on Windows 2000 to Windows 7.<

5条回答
  •  野性不改
    2020-11-27 18:36

    Should you wish to do it without the win32 extensions, you can use ctypes to call SHGetFolderPath:

    >>> import ctypes.wintypes
    >>> CSIDL_PERSONAL= 5       # My Documents
    >>> SHGFP_TYPE_CURRENT= 0   # Want current, not default value
    
    >>> buf= ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
    >>> ctypes.windll.shell32.SHGetFolderPathW(0, CSIDL_PERSONAL, 0, SHGFP_TYPE_CURRENT, buf)
    0
    >>> buf.value
    u'C:\\Documents and Settings\\User\\My Documents'
    

提交回复
热议问题