[python-win32] AppData directory on Windows?

David Robinow drobinow at gmail.com
Thu Sep 1 15:28:54 CEST 2011


On Thu, Sep 1, 2011 at 8:49 AM, Tim Golden <mail at timgolden.me.uk> wrote:
> On 01/09/2011 13:39, Kevin Walzer wrote:
>>
>> Hi,
>> I’m getting feedback from a Windows user that says my Tkinter app won’t
>> write data to an app temporary directory. Here’s my code:
>> sys.stderr=open(os.path.join(os.path.expanduser('~'), 'Application
>> Data', 'QuickWho', 'QuickWho_errors.txt'), 'w')
>
> This is a little brittle. I imagine it will at least break
> from non-English versions of Windows. It will also break
> for versions of Python prior to 2.5? when expanduser was
> expanded to include Windows.
 It also breaks if the user has set the HOME environment variable
(not uncommon for Cygwin users)
>
> The simplest approach is to use the APPDATA env var which
> goes back a few versions (certainly to XP, probably Win 2000).
 i.e., os.environ['APPDATA']

Also, 'open' will not create the 'QuickWho' directory for you. You may need:
 os.mkdir(os.path.join(os.environ['APPDATA'], 'QuickWho'))
>
> Alternatively, you can use the Windows Shell API to query the
> same thing -- this should go back even further if you've got
> someone using something truly prehistoric.


More information about the python-win32 mailing list