How can I get the path to the %APPDATA% directory in Python?

大兔子大兔子 提交于 2019-12-03 06:28:40

问题


How can I get the path to the %APPDATA% directory in Python?


回答1:


import os
print os.getenv('APPDATA')



回答2:


You may use 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. Malformed variable names and references to non-existing variables are left unchanged.

On Windows, %name% expansions are supported in addition to $name and ${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')



回答3:


You can try doing:

import os
path = os.getenv('APPDATA')
array = os.listdir(path)
print array


来源:https://stackoverflow.com/questions/13184414/how-can-i-get-the-path-to-the-appdata-directory-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!