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

后端 未结 3 687
悲&欢浪女
悲&欢浪女 2020-12-13 16:57

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

3条回答
  •  暖寄归人
    2020-12-13 17:24

    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')
    

提交回复
热议问题