如何使用 Python 查找 Windows 通用应用程序数据文件夹? [英] How do I find the Windows common application data folder using Python?

查看:89
本文介绍了如何使用 Python 查找 Windows 通用应用程序数据文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用程序存储一些数据以供所有用户访问.使用Python,如何找到数据应该去哪里?

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?

推荐答案

如果您不想为像 winpaths 这样的第三方模块添加依赖项,我建议您使用 Windows 中已有的环境变量:

If you don't want to add a dependency for a third-party module like winpaths, I would recommend using the environment variables already available in Windows:

>

  • 什么环境变量在 Windows 中可用吗?
  • 特别是您可能希望 ALLUSERSPROFILE 获取公共用户配置文件文件夹的位置,这是应用程序数据目录所在的位置.

    Specifically you probably want ALLUSERSPROFILE to get the location of the common user profile folder, which is where the Application Data directory resides.

    例如:

    C:\> python -c "import os; print os.environ['ALLUSERSPROFILE']"
    C:\Documents and Settings\All Users
    

    EDIT:查看 winpaths 模块,它使用 ctypes,因此如果您只想使用代码的相关部分而不安装 winpath,您可以使用它(显然为了简洁省略了一些错误检查).

    EDIT: Looking at the winpaths module, it's using ctypes so if you wanted to just use the relevant part of the code without installing winpath, you can use this (obviously some error checking omitted for brevity).

    import ctypes
    from ctypes import wintypes, windll
    
    CSIDL_COMMON_APPDATA = 35
    
    _SHGetFolderPath = windll.shell32.SHGetFolderPathW
    _SHGetFolderPath.argtypes = [wintypes.HWND,
                                ctypes.c_int,
                                wintypes.HANDLE,
                                wintypes.DWORD, wintypes.LPCWSTR]
    
    
    path_buf = wintypes.create_unicode_buffer(wintypes.MAX_PATH)
    result = _SHGetFolderPath(0, CSIDL_COMMON_APPDATA, 0, 0, path_buf)
    print path_buf.value
    

    示例运行:

    C:\> python get_common_appdata.py
    C:\Documents and Settings\All Users\Application Data
    

    这篇关于如何使用 Python 查找 Windows 通用应用程序数据文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆