Peter's Blog

Redefining the Impossible

Choosing a Directory


I don't normally put gui's on my python scripts but I am doing something for a client and cannot rely on them using the command line. All my script needs is the name of a directory to work with.

The following is a distillation of how to show the dialog for browsing for a directory under python and the win32com module.

   1  from win32com.shell import shell
   2  
   3  oNeedlesslyComplex = shell.SHBrowseForFolder(0, # parent HWND
   4                                  None, # root PIDL.
   5                                  "Choose directory to convert", # dialog title
   6                                  0, # flags
   7                                  None, # callback function
   8                                  None) # 'data' param for the callback
   9  
  10  if oNeedlesslyComplex[0] == None:
  11      pass
  12      # cancel pressed
  13  
  14  #
  15  # Get selected folder from weird return value.
  16  #
  17  strPath = shell.SHGetPathFromIDList(oNeedlesslyComplex[0])

win32com is powerful but little documented.


Filed under: python windows

Have Your Say

I welcome constructive comments or questions but I reserve the right to delete any comments that displease me.

Who are you?

(Optional) If you enter an email address here I might email you back. Your email address will not be sold to spammers or shown anywhere

What do you have to say?