How to get the user name, and to show a select directory dialog?

Joe Francia usenet at soraia.com
Thu May 22 17:58:22 EDT 2003


Arnal wrote:

> Hi!
> 
> I would like to obtain the name of the current logged user on a Windows
> machine, so I can get the "My documents" and "Desktop" folders.

#assumes stand alone win32api or
#ActiveState's Python installed

import win32api
win32api.GetUserName()

> I want to allow the user to specify the initial directory of the save
> command, and the open command. The "My documents" would be somehow basic to
> acceess, but I must have the username in the path (or can I access this
> folder directly with a function?). Or how to access the user name, else?

import win32api, os

MYDOCS = 'My Documents'
DESKTOP = 'Desktop'

home_path = win32api.GetEnvironmentVariable('USERPROFILE')
desktop_path = os.path.join(home_path, DESKTOP)
doc_path = os.path.join(home_path, MYDOCS)

> I would like, as well, to show a "select directory dialog". I can already
> put a select file and put file; I think that selecting a directory must be
> in the same module, isn't it?
> 

Hard to say when you don't mention what GUI toolkit you're using (PyQt, 
Tkinter, wxPython, direct win32 calls,...).

jf





More information about the Python-list mailing list