[英]How can I get the path to the %APPDATA% directory in Python?
How can I get the path to the %APPDATA%
directory in Python?如何在 Python 中获取%APPDATA%
目录的路径?
import os
print os.getenv('APPDATA')
You may use os.path.expandvars(path) :您可以使用os.path.expandvars(path) :
Return the argument with environment variables expanded.返回扩展了环境变量的参数。 Substrings of the form
$name
or${name}
are replaced by the value of environment variable name.$name
或${name}
形式的子字符串被环境变量 name 的值替换。 Malformed variable names and references to non-existing variables are left unchanged.格式错误的变量名和对不存在变量的引用保持不变。On Windows,
%name%
expansions are supported in addition to$name
and${name}
.在 Windows 上,除了$name
和${name}
之外,还支持%name%
扩展。
This comes handy when combining the expanded value with other path components.当将扩展值与其他路径组件组合时,这很方便。
Example:例子:
from os import path
sendto_dir = path.expandvars(r'%APPDATA%\Microsoft\Windows\SendTo')
dumps_dir = path.expandvars(r'%LOCALAPPDATA%\CrashDumps')
Although the question clearly asks about the Windows-specific %APPDATA%
directory, perhaps you have ended up here looking for a cross-platform solution for getting the application data directory for the current user , which varies by OS.尽管该问题明确询问了特定于 Windows 的%APPDATA%
目录,但也许您最终在这里寻找跨平台解决方案来获取当前用户的应用程序数据目录,该目录因操作系统而异。
As of Python 3.10, somewhat surprisingly, there is no built-in function to find this directory.从 Python 3.10 开始,有点令人惊讶的是,没有内置函数来查找此目录。 However, there are third-party packages, the most popular of which seems to be appdirs , which provides functions to retrieve paths such as:但是,也有第三方包,其中最流行的似乎是appdirs ,它提供了检索路径的功能,例如:
user_data_dir
)用户数据目录( user_data_dir
)user_config_dir
)用户配置目录( user_config_dir
)user_cache_dir
)用户缓存目录( user_cache_dir
)site_data_dir
)站点数据目录( site_data_dir
)site_config_dir
)站点配置目录( site_config_dir
)user_log_dir
)用户日志目录( user_log_dir
)You can try doing:你可以尝试这样做:
import os
path = os.getenv('APPDATA')
array = os.listdir(path)
print array
You can use module called appdata
.您可以使用名为appdata
模块。 It was developed to get access to different paths for your application, including app data folder.它的开发目的是访问应用程序的不同路径,包括应用程序数据文件夹。 Install it:安装它:
pip install appdata
And after that you can use it this way:之后你可以这样使用它:
from appdata import AppDataPaths
app_paths = AppDataPaths()
app_paths.app_data_path # for your app data path
app_paths.logs_path # for logs folder path for your application
It allows to to get not only app data folder and logs folder but has other features to manage paths like managing config files paths.它不仅允许获取应用程序数据文件夹和日志文件夹,还允许获取其他功能来管理路径,例如管理配置文件路径。 And it's customizable.它是可定制的。
Links:链接:
I printed this page and am gratfull for this.我打印了这一页并对此表示感谢。 Also tryed to configure "%APPDATA%"in this Dispositive, using: "notepad", doesn't knowing to archive the sugested condiguration.还尝试在这个 Dispositive 中配置“%APPDATA%”,使用:“notepad”,不知道要存档 sugested 配置。 Also copied sotoz and Aominé contribuiters.还复制了 sotoz 和 Aominé 贡献者。 tks.谢谢。
efk14it. efk14it。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.