Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the Windows common application data folder using Python?

I would like my application to store some data for access by all users. Using Python, how can I find where the data should go?

like image 570
David Sykes Avatar asked Mar 09 '09 15:03

David Sykes


People also ask

How do you locate the Application Data folder and how is it different from the Program Files folder?

You can either access it manually or by using the "AppData" variable name. You can view the AppData folder manually by going into your Users folder, which is there in the C drive. In my case, the path is C:\Users\ADMIN . Now you should be able to see the AppData folder in your User folder.

How do I find the application data folder in Windows 10?

To open the AppData folder on Windows 10, 8 & 7: Open File Explorer/Windows Explorer. Type %AppData% into the address bar and hit enter. Navigate to the required folder (Roaming or Local)

Where can I find application data?

Every Windows Operating System contains a folder called AppData - short for Application Data. The AppData folder in Windows 10 is a hidden folder located in C:\Users\<username>\AppData. It contains custom settings and other information that PC system applications need for their operation.


1 Answers

If you don't want to add a dependency for a third-party module like winpaths, I would recommend using the environment variables already available in Windows:

  • What environment variables are available in Windows?

Specifically you probably want ALLUSERSPROFILE to get the location of the common user profile folder, which is where the Application Data directory resides.

e.g.:

C:\> python -c "import os; print os.environ['ALLUSERSPROFILE']" C:\Documents and Settings\All Users 

EDIT: Looking at the winpaths module, it's using ctypes so if you wanted to just use the relevant part of the code without installing winpath, you can use this (obviously some error checking omitted for brevity).

import ctypes from ctypes import wintypes, windll  CSIDL_COMMON_APPDATA = 35  _SHGetFolderPath = windll.shell32.SHGetFolderPathW _SHGetFolderPath.argtypes = [wintypes.HWND,                             ctypes.c_int,                             wintypes.HANDLE,                             wintypes.DWORD, wintypes.LPCWSTR]   path_buf = wintypes.create_unicode_buffer(wintypes.MAX_PATH) result = _SHGetFolderPath(0, CSIDL_COMMON_APPDATA, 0, 0, path_buf) print path_buf.value 

Example run:

C:\> python get_common_appdata.py C:\Documents and Settings\All Users\Application Data 
like image 136
Jay Avatar answered Sep 18 '22 11:09

Jay