Trouble getting to windows My Documents directory

Mark Lawrence breamoreboy at yahoo.co.uk
Fri Jul 10 16:47:11 CEST 2015


On 10/07/2015 15:27, Mark Storkamp via Python-list wrote:
> I'm just learning Python, and I've run into trouble trying to change
> directory to the windows My Documents directory. There's likely a better
> way to do this, but this is what I've tried so far:
>
> ---------------------------------------------
> from tkinter import Tk
> from tkinter.filedialog import askopenfilename
>
> import os
>
> Tk().withdraw()
>
> sourcedir = os.environ['HOME']+"/Documents/"
> os.chdir(sourcedir)
> src = askopenfilename()
> if src == '' :
>      sys.exit()
> fin = open(src, mode='r')
> ## do stuff
> fin.close()
> -----------------------------------------------
>
> When this is run from IDLE, it works fine. But when I double click on
> the saved file to run it, it quits without ever showing the open file
> dialog box, and doesn't show any error message.
>
> The problem is with the os.environ['HOME'] call. If I comment it out
> (and fix up the surrounding code with an absolute path) then it runs.
> But then it won't work properly for other users.
>
> Interestingly enough, when I moved this to a Mac so I could post to
> Usenet, I discovered it works fine on the Mac. Only Windows seems to be
> the problem. Windows 7.
>
> Any thoughts or suggestions?
>

Home isn't defined in Windows but somehow is in IDLE?  From a command 
line enter "set home<cr>".  Doing that on my Windows 8.1 box shows:-
HOMEDRIVE=C:
HOMEPATH=\Users\Mark

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Python-list mailing list