[Tutor] Quickest way to find a folder on windows

Martin Walsh mwalsh at groktech.org
Tue Jul 26 14:37:38 CEST 2005

Jorge Louis De Castro wrote:
> Hi all,
> What is the quickest way to find/search a folder on windows? 
> Specifically, I want to find the 'My Documents' folder on different 
> machines but since this one lies in different user-specific folders (the 
> user account on XP) I'm being forced to traverse the whole drive looking 
> for it (though I can start from 'Documents and Settings' to prune the 
> search space)

While this doesn't go directly toward answering your fast search a 
folder question, it might get you closer for finding special folders -- 
and others on the list will certainly have a more concise solution

take a look at os.path.expanduser() and os.environ, which are available 

.>>> # on my setup (non-standard %HOME%), this works
.>>> os.path.join(os.path.expanduser('~'), 'My Documents')
'H:\\My Documents'

.>>> os.path.join(os.environ['USERPROFILE'], 'My Documents')
'C:\\Documents and Settings\\martin walsh\\My Documents'

as you can see, because of my non-standard setup, I get 2 different results

in this instance, Mark Hammond's win32 extensions can possibly help. Tim 
Golden has written the winshell module, uses win32 -- and includes a 
helper for "finding" the 'My Documents' folder -- it's available here:


 >>> import winshell # must have pywin32 & winshell installed
 >>> winshell.my_documents()
'H:\\my documents'


> Is there a faster way? I know this is very reliant on the windows search 
> API so maybe I'm stuck with my solution
> Any help is appreciated
> cheers
> j

More information about the Tutor mailing list