Path: csiph.com!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Irregular last line in a text file, was Re: Regular expressions Date: Tue, 3 Nov 2015 11:33:55 -0700 Lines: 24 Message-ID: References: <662g3blobme52hfoududj27err185v2npm@4ax.com> <20151102204237.6a78abdf@bigbox.christie.dr> <56382F33.8050905@gmail.com> <20151103055018.535e3e42@bigbox.christie.dr> <20151103105653.622d5e34@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de WjryJLyOj/TDlV8Grdcxng4XJw5fZoRMknkmu9bXseLg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.029 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'subject:text': 0.04; 'subject:file': 0.07; 'files.': 0.13; 'iterating': 0.16; 'loops': 0.16; 'received:209.85.213.176': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Regular': 0.16; 'subject:expressions': 0.16; 'wrote:': 0.16; '2015': 0.20; 'am,': 0.23; 'tim': 0.24; 'header:In-Reply-To:1': 0.24; 'message- id:@mail.gmail.com': 0.27; 'chase': 0.29; 'subject:last': 0.30; 'maybe': 0.33; 'tue,': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'nov': 0.35; 'something': 0.35; 'there': 0.36; 'lines': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'end': 0.39; 'to:addr:python.org': 0.40; 'valuable': 0.61; 'skip:n 10': 0.62; 'more': 0.63; 'wish': 0.71; 'special': 0.73; 'otten': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=JGesVKLyUS4tlKi64JGQhNGZOsMYmJs2ldEjjyAicLI=; b=whwq0zYvTWroPTkEKkRR151GXmtHRAgMHa+08G8qJ05wnFVV8/zv+O+V7DC0jLHmW0 emyQOIWFKqar9UGsC6+HJ4s6O0pOWuY1NpOU2VX3Gp1Nrn/S7uV/vTU9vQDnhRxljf/o FNwEFskoneh+uvznwVhmyVgzLeg9tv478rQLFFCeXZsM2u2FVa5rdU1OJpTJLiRzyyxy q0GuVV8FROCwE9MyhNDC7Oyw6tupmQFXa6F9rvekJBGCFz9zadeTfGN7I0jBx1lbpSb7 C6XPvU4Lilm9ICPeLrxnBuwbpo3FvOXY5XPj76PXk+r8rKlcE/Mv9HJMe5vyy7p7vBfw BNbA== X-Received: by 10.50.126.105 with SMTP id mx9mr13705712igb.93.1446575674566; Tue, 03 Nov 2015 10:34:34 -0800 (PST) In-Reply-To: <20151103105653.622d5e34@bigbox.christie.dr> 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: , Xref: csiph.com comp.lang.python:98183 On Tue, Nov 3, 2015 at 9:56 AM, Tim Chase wrote: > On 2015-11-03 16:35, Peter Otten wrote: >> I wish there were a way to prohibit such files. Maybe a special >> value >> >> with open(..., newline="normalize") f: >> assert all(line.endswith("\n") for line in f) >> >> to ensure that all lines end with "\n"? > > Or even more valuable to me: > > with open(..., newline="strip") as f: > assert all(not line.endswith(("\n", "\r")) for line in f) > > because I have countless loops that look something like > > with open(...) as f: > for line in f: > line = line.rstrip('\r\n') > process(line) What would happen if you read a file opened like this without iterating over lines?