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


Groups > comp.lang.python > #93640 > unrolled thread

Trouble getting to windows My Documents directory

Started byMark Storkamp <mstorkamp@yahoo.com>
First post2015-07-10 09:27 -0500
Last post2015-08-01 02:50 -0400
Articles 12 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  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

#93640 — Trouble getting to windows My Documents directory

FromMark Storkamp <mstorkamp@yahoo.com>
Date2015-07-10 09:27 -0500
SubjectTrouble getting to windows My Documents directory
Message-ID<mstorkamp-154B4A.09272010072015@88-209-239-213.giganet.hu>
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?

[toc] | [next] | [standalone]


#93642

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-07-10 15:47 +0100
Message-ID<mailman.398.1436539657.3674.python-list@python.org>
In reply to#93640
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

[toc] | [prev] | [next] | [standalone]


#93645

FromLaura Creighton <lac@openend.se>
Date2015-07-10 17:11 +0200
Message-ID<mailman.401.1436541099.3674.python-list@python.org>
In reply to#93640
Maybe HOMEPATH is what windows calls it?
http://libertyboy.free.fr/computing/reference/envariables/
(but maybe this is only for windows XP.  I don't have
a windows system, so I cannot test this.)

Laura

[toc] | [prev] | [next] | [standalone]


#93646

FromMRAB <python@mrabarnett.plus.com>
Date2015-07-10 16:12 +0100
Message-ID<mailman.402.1436541332.3674.python-list@python.org>
In reply to#93640
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'))

[toc] | [prev] | [next] | [standalone]


#93647

FromMark Storkamp <mstorkamp@yahoo.com>
Date2015-07-10 10:25 -0500
Message-ID<mstorkamp-C5E4BB.10250810072015@88-209-239-213.giganet.hu>
In reply to#93646
In article <mailman.402.1436541332.3674.python-list@python.org>,
 MRAB <python@mrabarnett.plus.com> wrote:

> 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'))

I thought there must be a way to pass the directory to askopenfilename, 
but I just hadn't figured out how yet.

The other suggestions for using HOMEPATH also worked for both Mac and 
Windows.

Thanks.

[toc] | [prev] | [next] | [standalone]


#93650

FromRustom Mody <rustompmody@gmail.com>
Date2015-07-10 10:48 -0700
Message-ID<657d89e9-7505-4a2b-b8e7-b7ce7b785a96@googlegroups.com>
In reply to#93647
On Friday, July 10, 2015 at 8:56:48 PM UTC+5:30, Mark Storkamp wrote:
>  MRAB  wrote:
> 
> > 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'))
> 
> I thought there must be a way to pass the directory to askopenfilename, 
> but I just hadn't figured out how yet.
> 
> The other suggestions for using HOMEPATH also worked for both Mac and 
> Windows.
> 
> Thanks.

According to http://serverfault.com/questions/29948/difference-between-profile-and-home-path
it seems %userprofile% is more current than %homepath%
[though windows arcana is not exactly my forte ;-) ]

[toc] | [prev] | [next] | [standalone]


#93651

FromRustom Mody <rustompmody@gmail.com>
Date2015-07-10 10:56 -0700
Message-ID<8fe697f8-c8e5-4858-888b-063b6b510656@googlegroups.com>
In reply to#93650
On Friday, July 10, 2015 at 11:18:57 PM UTC+5:30, Rustom Mody wrote:
> On Friday, July 10, 2015 at 8:56:48 PM UTC+5:30, Mark Storkamp wrote:
> >  MRAB  wrote:
> > 
> > > 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'))
> > 
> > I thought there must be a way to pass the directory to askopenfilename, 
> > but I just hadn't figured out how yet.
> > 
> > The other suggestions for using HOMEPATH also worked for both Mac and 
> > Windows.
> > 
> > Thanks.
> 
> According to http://serverfault.com/questions/29948/difference-between-profile-and-home-path
> it seems %userprofile% is more current than %homepath%
> [though windows arcana is not exactly my forte ;-) ]

which may need to be combined with something like:
home = 'HOME' if os.name=='posix' else 'USERPROFILE'
(see https://docs.python.org/2/library/os.html for os.name)

[toc] | [prev] | [next] | [standalone]


#93654

FromTim Chase <python.list@tim.thechases.com>
Date2015-07-10 09:46 -0500
Message-ID<mailman.405.1436561462.3674.python-list@python.org>
In reply to#93640
On 2015-07-10 09:27, Mark Storkamp via Python-list wrote:
> sourcedir = os.environ['HOME']+"/Documents/"

First, I'd do a couple things here to accommodate various systems to
make it cross-platform:

  sourcedir = os.path.join(
    os.path.expanduser('~'),
    "Documents"
    )

> os.chdir(sourcedir)
> src = askopenfilename()

Then, rather than changing to that directory, use

  src = askopenfilename(initialdir=sourcedir)

> 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.

If the above doesn't solve the problem, you might try writing the
sourcedir to a file so you can see what it thinks you're trying to
open.

> 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.

I suspect that inspecting that sourcedir will show what's going on,
and that the above tweaks will ameliorate the problem.

-tkc

[toc] | [prev] | [next] | [standalone]


#93660

Fromrandom832@fastmail.us
Date2015-07-10 20:24 -0400
Message-ID<mailman.408.1436574781.3674.python-list@python.org>
In reply to#93640
The My Documents directory is not guaranteed to be named "Documents". On
older versions of windows it was "My Documents", and on foreign versions
of windows it is a name in their language.

The correct way to get the path of this folder is, for example (couldn't
test since I'm on a mac right now)

import ctypes
from ctypes import wintypes

CSIDL_PERSONAL = 5

SHGetFolderPath = ctypes.windll.shell32.SHGetFolderPathW
pszPath = ctypes.create_unicode_buffer(wintypes.MAX_PATH)
SHGetFolderPath.argtypes = [wintypes.HWND, ctypes.c_int,
wintypes.HANDLE, wintypes.DWORD, LPWSTR]
SHGetFolderPath.restype = wintypes.HRESULT

hResult = SHGetFolderPath(0, CSIDL_PERSONAL, 0, 0, pszPath)

if hResult == 0:
  print pszPath.value
else
  raise OSError("Could not find My Documents directory")


Linux systems generally provide their own way to get it (e.g.
xdg-user-dirs), on OSX everything I can find indicates it really is
always ~/Documents .

[toc] | [prev] | [next] | [standalone]


#93662

FromChris Warrick <kwpolska@gmail.com>
Date2015-07-11 09:28 +0200
Message-ID<mailman.410.1436599708.3674.python-list@python.org>
In reply to#93640
On 11 July 2015 at 02:24,  <random832@fastmail.us> wrote:
> The My Documents directory is not guaranteed to be named "Documents". On
> older versions of windows it was "My Documents", and on foreign versions
> of windows it is a name in their language.

That’s not necessarily “older”.

Windows XP/7/8: My Documents
Windows Vista/8.1: Documents

As for localized versions, Vista and up do the translation in Windows
Explorer but use English names on disk.

…but even with all that, users (or their administrators) can move the
My Documents folder away from the home directory (to a file server,
for example).

-- 
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16

[toc] | [prev] | [next] | [standalone]


#93689

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2015-07-11 11:47 -0400
Message-ID<mailman.425.1436629661.3674.python-list@python.org>
In reply to#93640
On Sat, 11 Jul 2015 09:28:26 +0200, Chris Warrick <kwpolska@gmail.com>
declaimed the following:

>On 11 July 2015 at 02:24,  <random832@fastmail.us> wrote:
>> The My Documents directory is not guaranteed to be named "Documents". On
>> older versions of windows it was "My Documents", and on foreign versions
>> of windows it is a name in their language.
>
>That’s not necessarily “older”.
>
>Windows XP/7/8: My Documents
>Windows Vista/8.1: Documents
>
	On Win7, it DISPLAYS as "My Documents", but it exists as "Documents" in
the actual file system and on the command-line.

(snipped for brevity)

PS C:\Users\Wulfraed> ls

    Directory: C:\Users\Wulfraed

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d-r--          7/5/2015  10:40 PM            Documents
d-r--          7/4/2015  11:07 AM            Downloads

d-r--         3/12/2015   7:54 PM            Music

d-r--         3/12/2015   7:54 PM            Pictures

d-r--         3/12/2015   7:54 PM            Videos

PS C:\Users\Wulfraed>

	If I open that same directory using the Windows Explorer, I see a
cluster of

My Documents
My Music
My Pictures
My Videos

	Opening properties for those makes available a "Location" tab, by which
one can set the "My xxx" to /any/ directory desired.

	For easing transition of old software, there ARE hidden "junction
points" in the file system that redirect to the new locations:

C:\Users\Wulfraed\Documents>dir my*
 Volume in drive C is OS
 Volume Serial Number is 2004-C3FA

 Directory of C:\Users\Wulfraed\Documents

06/11/2015  08:18 AM    <DIR>          My Barnes & Noble eBooks
06/09/2013  07:39 PM    <DIR>          My Data Sources
06/09/2013  05:01 PM    <DIR>          My Digital Editions
12/19/2013  09:19 PM    <DIR>          My Downloads
06/05/2015  09:05 AM    <DIR>          My eBooks
06/09/2013  07:44 PM    <DIR>          My Garmin
06/09/2013  07:44 PM    <DIR>          My Movies
06/09/2013  07:45 PM    <DIR>          My Scanner Data Files
               0 File(s)              0 bytes
               8 Dir(s)  1,499,112,079,360 bytes free

C:\Users\Wulfraed\Documents>dir /AL
 Volume in drive C is OS
 Volume Serial Number is 2004-C3FA

 Directory of C:\Users\Wulfraed\Documents

06/01/2013  03:45 PM    <JUNCTION>     My Music [C:\Users\Wulfraed\Music]
06/01/2013  03:45 PM    <JUNCTION>     My Pictures
[C:\Users\Wulfraed\Pictures]
06/01/2013  03:45 PM    <JUNCTION>     My Videos [C:\Users\Wulfraed\Videos]
               0 File(s)              0 bytes
               3 Dir(s)  1,499,112,079,360 bytes free

C:\Users\Wulfraed\Documents>cd ..

C:\Users\Wulfraed>dir /AL
 Volume in drive C is OS
 Volume Serial Number is 2004-C3FA

 Directory of C:\Users\Wulfraed

06/01/2013  03:45 PM    <JUNCTION>     Application Data
[C:\Users\Wulfraed\AppData\Roaming]
06/01/2013  03:45 PM    <JUNCTION>     Cookies
[C:\Users\Wulfraed\AppData\Roaming\Microsoft\Wind
ows\Cookies]
06/01/2013  03:45 PM    <JUNCTION>     Local Settings
[C:\Users\Wulfraed\AppData\Local]
06/01/2013  03:45 PM    <JUNCTION>     My Documents
[C:\Users\Wulfraed\Documents]
06/01/2013  03:45 PM    <JUNCTION>     NetHood
[C:\Users\Wulfraed\AppData\Roaming\Microsoft\Windows\Network Shortcuts]
06/01/2013  03:45 PM    <JUNCTION>     PrintHood
[C:\Users\Wulfraed\AppData\Roaming\Microsoft\Windows\Printer Shortcuts]
06/01/2013  03:45 PM    <JUNCTION>     Recent
[C:\Users\Wulfraed\AppData\Roaming\Microsoft\Windows\Recent]
06/01/2013  03:45 PM    <JUNCTION>     SendTo
[C:\Users\Wulfraed\AppData\Roaming\Microsoft\Windows\SendTo]
06/01/2013  03:45 PM    <JUNCTION>     Start Menu
[C:\Users\Wulfraed\AppData\Roaming\Microsoft\Windows\Start Menu]
06/01/2013  03:45 PM    <JUNCTION>     Templates
[C:\Users\Wulfraed\AppData\Roaming\Microsoft\Windows\Templates]
               0 File(s)              0 bytes
              10 Dir(s)  1,499,112,079,360 bytes free

C:\Users\Wulfraed>
C:\>dir "Users\Wulfraed\My Documents"
 Volume in drive C is OS
 Volume Serial Number is 2004-C3FA

 Directory of C:\Users\Wulfraed\My Documents

File Not Found

C:\>

	I'd like to point out that I did NOT type the My Documents; I type My
and tab completion found the junction point name to fill in...

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#94832

Fromrandom832@fastmail.us
Date2015-08-01 02:50 -0400
Message-ID<mailman.1126.1438411862.3674.python-list@python.org>
In reply to#93640
On Sat, Jul 11, 2015, at 03:28, Chris Warrick wrote:
> That’s not necessarily “older”.
> 
> Windows XP/7/8: My Documents
> Windows Vista/8.1: Documents

Mine's called Documents on 7 and 8. Is the system you got this
information from a new install or an upgrade? (It may also depend on
whether it's a new *user profile* or one from before the upgrade)

> As for localized versions, Vista and up do the translation in Windows
> Explorer but use English names on disk.
> 
> …but even with all that, users (or their administrators) can move the
> My Documents folder away from the home directory (to a file server,
> for example).

Yeah, the real point is that it can be an arbitrary location, but I was
just illustrating that even in *default* circumstances it won't always
be Documents

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web