tags:

views:

248

answers:

3

In C++ it's not too hard to get the full pathname to the folder that the shell calls "My Documents" in Windows XP and Windows 7 and "Documents" in Vista; see http://stackoverflow.com/questions/2414828/get-path-to-my-documents

Is there a simple way to do this in Python?

A: 

I don't recall the exact 'getenv' format, but the variable you're looking for is USERPROFILE to which you can append /My Documents or /Documents as necessary. Since this query is specific to Windows, ascertaining the appropriate version should be trivial.

(after a quick search) it seems to be

import os
mydocuments = os.getenv('USERPROFILE') + '/My Documents'

Note that HOMEPATH may also work (see first comment to your original query)

[edit]
also note that os.path.expanduser('~') returns the \Documents and Settings\username directory under windows, unix and mac osx (but not mac os) making it far more convenient when you move to another os/architecture
[/edit]

Why the downvotes, BTW? Since the first comment does not appear to be valid, is it only because of the miss between 'My Documents' and 'Documents'?

KevinDTimm
I am pretty sure this is the wrong way to go, as pretty much any non-English version of Windows will have this method crap out if you use it like that, given the fact that said directory name is subject to localization.
Stigma
@Stigma, I don't know if the directory name itself is subject to translation or only the name shown in the shell. However I do know it changed between XP and Vista, now it's just `Documents`. I also know that the folder can be reassigned using TweakUI, so this solution isn't universal.
Mark Ransom
A: 

I am not sure if this would work in Windows, but this is how I do it in UNIX -

from os.path import abspath
abspath(".")

This gives the absolute path of the current directory. See what happens when you give r'My\ Documents' as the dirname.

MovieYoda
Sorry, it just assumes `My Documents` is a file in the current directory. Wrong answer altogether.
Mark Ransom
oh! have you "slashed" - My Documents? i.e. owing to the presence of space it might get confused. Have you made it raw string? i.e. try in this form r"My\ Documents" or r"My\\ Documents". If it still doesn't work. Sorry mate, to have wasted your time.
MovieYoda
I don't think you can safely assume that the current directory is set to something in the "My Documents" folder.
martineau
+8  A: 

You could use the ctypes module to get the "My Documents" directory:

import ctypes

dll = ctypes.windll.shell32
buf = ctypes.create_unicode_buffer(300)
dll.SHGetSpecialFolderPathW(None, buf, 0x0005, False)
print(buf.value)

Source: http://bugs.python.org/issue1763#msg62242

offsound
I'd give a +1, but using the ANSI version may be limiting on directories that use characters outside the default code page.
Adrian McCarthy
@Adrian McCarthy, thanks I didn't notice that. I've changed my answer to use the unicode version instead.
offsound
I presume the magic constant 0x0005 is CSIDL_PERSONAL. Is the magic constant 300 documented somewhere, or is it just MAX_PATH with some arbitrary padding added?
Mark Ransom
@Mark Ransom, yes, the hard coded 300 is MAX_PATH + 40 bytes of padding.
offsound
FWIW, a `SHGetSpecialFolderPathW()` call returns True if successful, False otherwise, so that can be checked to determine if anything useful was placed in `buf.value`.
martineau