How do I find the Windows common application data folder using Python?

后端 未结 6 871
谎友^
谎友^ 2020-11-30 05:55

I would like my application to store some data for access by all users. Using Python, how can I find where the data should go?

6条回答
  •  星月不相逢
    2020-11-30 06:16

    From http://snipplr.com/view.php?codeview&id=7354

    homedir = os.path.expanduser('~')
    
    # ...works on at least windows and linux. 
    # In windows it points to the user's folder 
    #  (the one directly under Documents and Settings, not My Documents)
    
    
    # In windows, you can choose to care about local versus roaming profiles.
    # You can fetch the current user's through PyWin32.
    #
    # For example, to ask for the roaming 'Application Data' directory:
    #  (CSIDL_APPDATA asks for the roaming, CSIDL_LOCAL_APPDATA for the local one)
    #  (See microsoft references for further CSIDL constants)
    try:
        from win32com.shell import shellcon, shell            
        homedir = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)
    
    except ImportError: # quick semi-nasty fallback for non-windows/win32com case
        homedir = os.path.expanduser("~")
    

    To get the app-data directory for all users, rather than the current user, just use shellcon.CSIDL_COMMON_APPDATA instead of shellcon.CSIDL_APPDATA.

提交回复
热议问题