Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'subject:getting': 0.07; 'path)': 0.09; 'src': 0.09; 'properly': 0.15; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'idle,': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'open(src,': 0.16; 'quits': 0.16; 'received:192.168.1.4': 0.16; 'runs.': 0.16; 'subject:windows': 0.16; 'wrote:': 0.16; 'directory.': 0.18; 'skip:a 60': 0.18; 'thoughts': 0.18; 'windows': 0.20; 'fix': 0.21; 'tkinter': 0.22; 'trying': 0.22; 'seems': 0.23; 'absolute': 0.23; 'tried': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'skip:- 40': 0.25; "i've": 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'error': 0.27; 'moved': 0.27; 'idea': 0.28; 'fine': 0.28; 'dialog': 0.29; "i'm": 0.30; 'code': 0.30; 'call.': 0.30; 'users.': 0.31; 'post': 0.31; 'run': 0.33; 'problem': 0.33; 'message.': 0.33; 'open': 0.33; 'file': 0.34; 'trouble': 0.35; 'could': 0.35; 'saved': 0.35; 'comment': 0.35; 'problem.': 0.35; 'but': 0.36; '(and': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'mac': 0.37; "won't": 0.38; 'stuff': 0.38; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'mark': 0.40; 'ever': 0.60; 'documents': 0.61; 'show': 0.62; 'python-list': 0.66; 'box,': 0.67; 'click': 0.76; 'discovered': 0.83; 'fin': 0.84; 'mac.': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=JOrGyJ+b c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=AGkejYDSTmIA:10 a=IkcTkHD0fZMA:10 a=f8JYIXheacdQ3mx_YtUA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Date: Fri, 10 Jul 2015 16:12:17 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Trouble getting to windows My Documents directory References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1436541332 news.xs4all.nl 2897 [2001:888:2000:d::a6]:53709 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93646 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'))