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

后端 未结 5 993
终归单人心
终归单人心 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:32

    You can do it with the pywin32 extensions:

    from win32com.shell import shell, shellcon
    print shell.SHGetFolderPath(0, shellcon.CSIDL_MYPICTURES, None, 0)
    # prints something like C:\Documents and Settings\Username\My Documents\My Pictures
    # (Unicode object)
    

    Check shellcon.CSIDL_xxx for other possible folders.

    I think using pywin32 is the best way. Else you'd have to use ctypes to access the SHGetFolderPath function somehow (other solutions might be possible but these are the ones I know).

提交回复
热议问题