pythonappdata

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


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


Solution

  • If you want AppData\Roaming

    import os
    print(os.getenv('APPDATA'))
    

    If you are looking for AppData\Local, then use

    import os
    print(os.getenv('LOCALAPPDATA'))
    

    For AppData\Local\Temp you can get it in this way, which also makes your code portable accross platforms

    import tempfile
    print(tempfile.gettempdir())
    

    For the differences:

    See also this answer.