Peter's Blog

Redefining the Impossible

Choosing a Directory

Unknown post id lite|slide|etc

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])
Toggle Line Numbers

win32com is powerful but little documented.


Filed under: python windows

Sorry but comments on this post are now closed.