Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.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.052 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'importing': 0.05; 'binary': 0.07; 'bytes,': 0.09; 'subject:files': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'windows': 0.15; '"in': 0.16; "(it's": 0.16; '.py': 0.16; '__future__': 0.16; 'clear.': 0.16; 'division,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'open()': 0.16; 'personally,': 0.16; 'unix,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'later': 0.20; 'import': 0.22; 'cc:addr:python.org': 0.22; 'either.': 0.24; 'unicode': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'easier': 0.31; 'go.': 0.31; 'sep': 0.31; 'file': 0.32; 'quite': 0.32; 'text': 0.33; 'open': 0.33; 'fri,': 0.33; 'trouble': 0.34; "i'd": 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'in.': 0.36; 'programming,': 0.36; 'subject:?': 0.36; 'too': 0.37; 'two': 0.37; 'clear': 0.37; 'easiest': 0.38; 'files': 0.38; 'short': 0.38; 'sure': 0.39; 'even': 0.60; 'skip:u 10': 0.60; 'read': 0.60; 'worry': 0.60; 'most': 0.60; 'conversion': 0.61; 'today,': 0.61; 'simple': 0.61; 'save': 0.62; "you'll": 0.62; 'soon': 0.63; 'more': 0.64; 'long- term': 0.74; 'obvious': 0.74; 'yourself': 0.78; 'truth': 0.81; 'easier,': 0.84; 'everywhere.': 0.84; 'tossing': 0.84; '2020': 0.91; 'subject:Best': 0.91; 'write:': 0.91; 'to:none': 0.92 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:cc :content-type; bh=nY3HxHKFp9okD9XN8rcUqGD/92wEdgxeCTJMeT5yBow=; b=d8UORHSTt9ZHvLs3teUrvVWKzyO00yp125d/QM50qvajK9sLotIZ1HKO8+/qH1Vg52 hNvthhTcO/EUI1GYRU2WpFrqPB2PHj47D+snTTSUd1nUWvOPJ9oQAUBWfz9Ge1TYCJIz sTg3GHJx1kOCpta22MLcgWTxzUGG3xWnhupZw2ZFIGeSHXYL5xi/LSBrIzcaUQNNKuUA KzxJS2r+lghu11UJX4jAh3cg+7qS36t5NINVTro1j6bSNhph10SMS1cCxgqEL01BYzjZ vPO8Izt8jtRFmoapM4TKGRQPZgjweIheOYBZXsWVlv7/9FW4oQdILe62dXoKMv6Ik7y6 0xVw== MIME-Version: 1.0 X-Received: by 10.42.231.9 with SMTP id jo9mr13529197icb.12.1411058262847; Thu, 18 Sep 2014 09:37:42 -0700 (PDT) In-Reply-To: <0081d786-d218-495a-b79f-8882e786d7d0@googlegroups.com> References: <0081d786-d218-495a-b79f-8882e786d7d0@googlegroups.com> Date: Fri, 19 Sep 2014 02:37:42 +1000 Subject: Re: Best practice for opening files for newbies? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1411058265 news.xs4all.nl 2919 [2001:888:2000:d::a6]:55686 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78024 On Fri, Sep 19, 2014 at 2:19 AM, wrote: > So: there are way too many ways to open a simple file to read or write a bit of text (or binary): > > open() Personally, I'd just use this, all the way through - and not importing from io, either. But others may disagree. Be clear about what's text and what's bytes, everywhere. When you do make the jump to Py3, you'll have to worry about text files vs binary files, and if you need to support Windows as well as Unix, you need to get that right anyway, so just make sure you get the two straight. Going Py3 will actually make your job quite a bit easier, there; but even if you don't, save yourself a lot of trouble later on by keeping the difference very clear. And you can save yourself some more conversion trouble by tossing this at the top of every .py file you write: from __future__ import print_function, division, unicode_literals But mainly, just go with the simple open() call and do the job the easiest way you can. And go Py3 as soon as you can, because ... > because that is still what most of the code "in the wild" is in. ... this statement isn't really an obvious truth any more (it's hard to say what "most" code is), and it's definitely not going to remain so for the long-term future. For people learning Python today, unless they plan on having a really short career in programming, more of their time will be after 2020 than before it, and Python 3 is the way to go. Plus, it's just way WAY easier to get Unicode right in Py3 than in Py2. Save yourself the hassle! ChrisA