You're right that you should not be writing into your Program Files directory. And really, that's the solution to your program: change your script to stop doing that.
But if you want to know where your files are going, when you try to write to Program Files (on Vista or later, with UAC not disabled):
- If you're being run as Administrator, and not with XP/2003 or earlier emulation, the files get written directly to the path specified.
- If your app fits the magic triggers to look like an installer program (see here for the Vista docs), the files get written directly to the path specified.
- If your app is in Microsoft's compatibility database, the paths are mapped through the entires in the compatibility database (which doesn't seem to be documented anywhere).
- Otherwise, your paths are passed through UAC file virtualization, which means attempts to write to protected directories like
Program Files
end up somewhere under the user's app data directory—to your program, it looks like Program Files, but to, say, Explorer or cmd.exe it doesn't.
I believe the location is not guaranteed nor documented, and at one point it moved from the app's app data directory for the user to a per-app directory under a different special app data directory for the user, but at least on one Windows 7 machine it's this:
C:\Users\horriblyUnpythonic\AppData\Local\VirtualStore\Program Files (x86)\ProgramName\temp\filename.db
There are a lot of blog posts out there if you search for "UAC file virtualization". Most of them are written from the point of view of building custom installers, like this one (because, as you can imagine, this is a serious problem for a custom installer), but they mostly all get the ideas across, and provide lots of details, and add links for further reading.
If you only experienced this problem migrating from Vista to 7, or from 32-bit Vista to 64-bit Vista, this Microsoft blog is the only place I've seen that got that far into the details… except that the images and most of the discussion seem to have disappeared at some point.