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

5

97

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

Willywillynilly answered 1/11, 2012 at 19:48 Comment(0)
J
170

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:

  • Roaming is the folder that would be synchronized with a server if you logged into a domain with a roaming profile (enabling you to log into any computer in a domain and access your favorites, documents, etc. Firefox stores its information here, so you could even have the same bookmarks between computers with a roaming profile.

  • Local is the folder that is specific to that computer - any information here would not be synchronized with a server. This folder is equivalent in Windows XP to C:\Documents and Settings\User\Local Settings\Application Data.

See also this answer.

Jules answered 1/11, 2012 at 19:52 Comment(2)
Note that this returns AppData\Roaming. You need %LOCALAPPDATA% if you are looking for AppData\Local.Boatyard
If you are looking for AppData\LocalLow you need to use ..\LocalLow as there is no environment variable as far as I can find.Lyophobic
P
38

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')
Psalmbook answered 27/9, 2018 at 10:1 Comment(1)
These days pathlib is often more convenient, but, unfortunately it does not implement expandvars(), although it does implement expanduser().Gainly
F
35

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.

As of Python 3.11, somewhat surprisingly, there is no built-in function to find this directory. However, there are third-party packages, one of them being platformdirs, which provides functions to retrieve paths such as:

  • 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)
Fernandina answered 12/2, 2021 at 10:15 Comment(2)
this answer should have more upvotesUnreel
It says, platformdirs should be used instead of appdirs, since last is deprecated by authors (reason: platformdirs is more maintained fork of appdir)Benner
O
2

You can try doing:

import os
path = os.getenv('APPDATA')
array = os.listdir(path)
print array
Overplay answered 2/12, 2017 at 18:40 Comment(1)
This is essentially the same as the accepted answer given five years before. OP asked how to get the path, not how to enumerate the contents of a directory.Psalmbook
T
-2

You can use module called 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:

  1. Read the Docs - documentation.
  2. GitHub - source code.
  3. PyPI - package manager (pip).
Tedder answered 19/12, 2021 at 12:53 Comment(1)
As the author of appdata, you should have a look at How to not be a spammer: it says: "if you mention your product [...], you must disclose your affiliation in your post."Psalmbook

© 2022 - 2024 — McMap. All rights reserved.