Retrieving common Windows folder names with Python

Often using Windows, you'll need to get the names of special folders, like "App Data," "Local App Data," "My Documents," and "My Pictures." This is especially true in Vista, where the recommended practice of writing app-specific data to the (Local) App Data directory is being enforced.

You can do this using the win32com.shell.shell module in the pywin32 module:

from win32com.shell import shell, shellcon

my_pics = shell.SHGetFolderPath(0,
                       shellcon.CSIDL_MYPICTURES,
                       0,
                       0)

In addition, I've written a simple module that does this. It depends only on ctypes, and provides a simpler interface to the Windows shell API. Usage:

import winpaths

my_pics = winpaths.get_my_pictures()
my_docs = winpaths.get_my_documents()

You can install it using easy_install:

easy_install winpaths

Here's the link on pypi.

Here are the folders it provides access to:

    get_local_appdata — Local App Data folder for current user
    get_appdata — App Data folder for current user
    get_desktop — Desktop folder for current user
    get_programs — Programs directory under Start menu
    get_admin_tools — Admin Tools directory under Start menu
    get_common_admin_tools — Start Menu -> Admin Tools
    get_common_appdata Common App Data folder
    get_common_documents Common My Documents folder
    get_cookies — Cookies folder for current user
    get_history — History
    get_internet_cache — Internet cache
    get_my_pictures — My Pictures folder for current user
    get_personal — My Documents
    get_my_documents — alias for get_personal
    get_program_files — Program Files under root drive
    get_program_files_common — Program Files -> Common Files
    get_systemUse care and discretion
    get_windowsUse care and discretion
    get_favorites — Favorites folder for current user
    get_startup — Startup folder under Start Menu -> Programs
    get_recent — Recent files for current user

[Update] — winpaths now has a permanent page here.

4 comments to Retrieving common Windows folder names with Python

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>