python save files to user folder (Windows)

file, python, windows

Solution

The doc says, the following code is expanded to the user's home directory on windows too..

homeDir = os.path.expanduser("~")

From the documentation:

On Windows, HOME and USERPROFILE will be used if set, otherwise a combination of HOMEPATH and HOMEDRIVE will be used. An initial ~user is handled by stripping the last directory component from the created user path derived above.

Also look into this for using Win32APIs

Problem

I have this program in Python which should save text files to a folder inside the "My Documents" of the user. The problem is that the program doesn't know the username, or the Windows version to know the correct path. How could I get this done?

Original source

Related problems