Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #93646

Re: Trouble getting to windows My Documents directory

Date 2015-07-10 16:12 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: Trouble getting to windows My Documents directory
References <mstorkamp-154B4A.09272010072015@88-209-239-213.giganet.hu>
Newsgroups comp.lang.python
Message-ID <mailman.402.1436541332.3674.python-list@python.org> (permalink)

Show all headers | View raw


On 2015-07-10 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?
>
Try os.path.expanduser(r'~\Documents').

It's a bad idea to use os.chdir; it's better to use absolute paths:

src = askopenfilename(initialdir=os.path.expanduser(r'~\Documents'))

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Trouble getting to windows My Documents directory Mark Storkamp <mstorkamp@yahoo.com> - 2015-07-10 09:27 -0500
  Re: Trouble getting to windows My Documents directory Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-07-10 15:47 +0100
  Re: Trouble getting to windows My Documents directory Laura Creighton <lac@openend.se> - 2015-07-10 17:11 +0200
  Re: Trouble getting to windows My Documents directory MRAB <python@mrabarnett.plus.com> - 2015-07-10 16:12 +0100
    Re: Trouble getting to windows My Documents directory Mark Storkamp <mstorkamp@yahoo.com> - 2015-07-10 10:25 -0500
      Re: Trouble getting to windows My Documents directory Rustom Mody <rustompmody@gmail.com> - 2015-07-10 10:48 -0700
        Re: Trouble getting to windows My Documents directory Rustom Mody <rustompmody@gmail.com> - 2015-07-10 10:56 -0700
  Re: Trouble getting to windows My Documents directory Tim Chase <python.list@tim.thechases.com> - 2015-07-10 09:46 -0500
  Re: Trouble getting to windows My Documents directory random832@fastmail.us - 2015-07-10 20:24 -0400
  Re: Trouble getting to windows My Documents directory Chris Warrick <kwpolska@gmail.com> - 2015-07-11 09:28 +0200
  Re: Trouble getting to windows My Documents directory Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-07-11 11:47 -0400
  Re: Trouble getting to windows My Documents directory random832@fastmail.us - 2015-08-01 02:50 -0400

csiph-web