Find system folder locations in Python

后端 未结 5 840
鱼传尺愫
鱼传尺愫 2020-12-09 06:08

I am trying to find out the location of system folders with Python 3.1. For example \"My Documents\" = \"C:\\Documents and Settings\\User\\My Documents\", \"Program Files\"

5条回答
  •  执笔经年
    2020-12-09 06:29

    Here's an alternative win32com approach because WScript.Shell "special folders do not work in all language locales, a preferred method is to query the value from User Shell folders" (ref):

    >>> ID = 48
    >>> shapp = win32com.client.Dispatch("Shell.Application")
    >>> shapp.namespace(ID).self.path
    'C:\\Users\\mattw\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Administrative Tools'
    

    The ID number comes from MSDN ShellSpecialFolderConstants Enumeration. I converted that list to csv for easy use and wrote a short python script demoing that, gist here.

    Special thanks to Mr Chimp for starting this off. I relied heavily on his answer and references to get started.

提交回复
热议问题