Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'value,': 0.03; 'subject:Python': 0.05; '"""': 0.05; 'context': 0.05; 'sysadmin': 0.05; 'type,': 0.07; 'core,': 0.09; 'imho.': 0.09; 'subject:Why': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'daemons': 0.16; 'dump': 0.16; 'general.': 0.16; 'invoking': 0.16; 'roy': 0.16; 'those,': 0.16; 'wrote:': 0.17; 'directory.': 0.17; 'mostly': 0.20; 'cc:2**0': 0.23; 'programming': 0.23; 'absolute': 0.23; "i've": 0.23; 'somewhere': 0.24; 'cc:no real name:2**0': 0.24; 'command': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'core': 0.27; 'message-id:@mail.gmail.com': 0.27; 'fixed': 0.28; 'run': 0.28; 'sensible': 0.29; 'url:mailman': 0.29; 'skip:_ 10': 0.29; 'source': 0.29; 'class': 0.29; 'that.': 0.30; 'relative': 0.30; 'url:python': 0.32; 'file': 0.32; 'running': 0.32; 'url:listinfo': 0.32; 'goes': 0.33; 'received:google.com': 0.34; 'self': 0.34; 'done': 0.34; 'remote': 0.35; 'sometimes': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'really': 0.36; 'created': 0.36; 'tool': 0.36; 'but': 0.36; 'url:org': 0.36; 'possible': 0.37; 'quite': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'some': 0.38; 'sure': 0.38; 'gives': 0.39; 'header:Received:5': 0.40; 'url:mail': 0.40; 'think': 0.40; 'more': 0.63; 'making': 0.64; 'smith': 0.71; 'article': 0.78; 'designed,': 0.84; 'troubles': 0.84; 'subject:remember': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=nHBaEbE/TDfoRKmfXBaTFhBSiLAsFNWkDR3uAV56er8=; b=Te9DxDs/TjcfcVV5TgN3SmpY+ZapYM15jon4lpxrGF2QeZe+3iZumxY1bgiRVTif4Q 9EPEJz7y/rSIV3o124wkhWztvARA0Ovz+zYmrYfUH1KTqyPUCpjQDJLqmHYSH402hPMt gSLqbnfbD7IlstHD4fNSZo5KdT3ecVY3pAHnydQ4LzPJsJSLsnKQcMdUMnJ+6OJCnrKB xvlFNs6tTe4LifsBcCsS3SDRHvwwZisVD+yuLTbrNR/SEade1b8Pj9mapI9ZDhsZG53R wG4yIosUKsc5rWOMLyf0ODejqjR893Q8eMRoTItQ9HOQL1D0a0jFFnCRmLGQOONRimzk bDtQ== MIME-Version: 1.0 In-Reply-To: References: <5032381a$0$29978$c3e8da3$5496439d@news.astraweb.com> Date: Mon, 20 Aug 2012 15:15:37 +0100 Subject: Re: Why doesn't Python remember the initial directory? From: andrea crotti To: Roy Smith Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345472139 news.xs4all.nl 6888 [2001:888:2000:d::a6]:39126 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27495 2012/8/20 Roy Smith : > In article , > Walter Hurry wrote: > >> It is difficult to think of a sensible use for os.chdir, IMHO. > > It is true that you can mostly avoid chdir() by building absolute > pathnames, but it's often more convenient to just cd somewhere and use > names relative to that. Fabric (a very cool tool for writing remote > sysadmin scripts), gives you a cd() command which is a context manager, > making it extra convenient. > > Also, core files get created in the current directory. Sometimes > daemons will cd to some fixed location to make sure that if they dump > core, it goes in the right place. > > On occasion, you run into (poorly designed, IMHO) utilities which insist > of reading or writing a file in the current directory. If you're > invoking one of those, you may have no choice but to chdir() to the > right place before running them. > -- > http://mail.python.org/mailman/listinfo/python-list I've done quite a lot of system programming as well, and changing directory is only a source of possible troubles in general. If I really have to for some reasons I do this class TempCd: """Change temporarily the current directory """ def __init__(self, newcwd): self.newcwd = newcwd self.oldcwd = getcwd() def __enter__(self): chdir(self.newcwd) return self def __exit__(self, type, value, traceback): chdir(self.oldcwd) with TempCd('/tmp'): # now working in /tmp # now in the original So it's not that hard to avoid problems..