Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Oscar Benjamin Newsgroups: comp.lang.python Subject: Re: getting fileinput to do errors='ignore' or 'replace'? Date: Fri, 4 Dec 2015 09:00:32 +0000 Lines: 46 Message-ID: References: <8336jcxi2m.ln2@news.ducksburg.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de c201zAMI7jn162a9eAJ9YwBzf83OSkLO8oFSVdPEEalA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.05; "subject:' ": 0.07; 'subject:getting': 0.07; 'cc:addr:python-list': 0.09; 'benjamin': 0.09; 'closed.': 0.09; 'files:': 0.09; 'subject:ignore': 0.09; 'whichever': 0.09; '2.7': 0.13; '>>>': 0.15; '"terry': 0.16; 'cc:name:python list': 0.16; 'consume': 0.16; 'defaulting': 0.16; 'email addr:udel.edu>': 0.16; 'email name:<tjreedy': 0.16; 'iteration': 0.16; 'iterators': 0.16; 'iterators,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reedy"': 0.16; 'statement.': 0.16; 'wrote:': 0.16; '>': 0.18; 'email addr:gmail.com>': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'dec': 0.23; 'slightly': 0.23; 'header:In-Reply-To:1': 0.24; 'module': 0.25; "doesn't": 0.26; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; "skip:' 10": 0.28; 'fine': 0.28; 'convert': 0.29; 'statement': 0.32; 'wrap': 0.33; 'open': 0.33; 'correctly': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'skip:c 30': 0.35; 'replace': 0.35; 'received:209.85': 0.36; '(and': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'files': 0.38; 'end': 0.39; 'does': 0.39; 'easily': 0.39; 'your': 0.60; 'managers': 0.63; 'skip:\xc2 10': 0.67; 'guaranteed': 0.67; 'oscar': 0.84 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=2u3FNH0hRy0aImTcfkylLLt8VI6ZDnr6Imgw04U8gOY=; b=ZVue2+7iXjsJj1P8yDQv+BBevzhnxrXRzYOGqvxSrAJ77RNSoB+tkmsEh3lShmQy0c u2GE19CSPqg19CWzbKwwSjo0rc02GGLA0linhf462HJdgY4ZNJjasW4C2SKp45qI7DzZ l+WryhmI6f32/czgZK1gvCsY2wqtD0KRbz+x3bXXLqQ57TWhkRi+50oawGyaWdWfNmFB RE5j9IdE7uzdwrHkSHQX5qpc1FrGGHeWIJKwuWz89WIreHDSN66K7aqIKuZXH8vhmDER o8gDEyV8xcVT/+j2/3dHIOfhXGz+BcuJceoDoCSLNId/0+L8bdOGDXBpHYUuZoa0Q4u5 TknA== X-Received: by 10.112.148.198 with SMTP id tu6mr7146514lbb.39.1449219633089; Fri, 04 Dec 2015 01:00:33 -0800 (PST) In-Reply-To: X-Content-Filtered-By: Mailman/MimeDel 2.1.20+ 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:99999 On 4 Dec 2015 08:36, "Serhiy Storchaka" wrote: > > On 04.12.15 00:26, Oscar Benjamin wrote: >> >> On 3 Dec 2015 16:50, "Terry Reedy" wrote: >>> >>> fileinput is an ancient module that predates iterators (and generators) >> >> and context managers. Since by 2.7 open files are both context managers and >> line iterators, you can easily write your own multi-file line iteration >> that does exactly what you want. At minimum: >>> >>> >>> for file in files: >>> with codecs.open(file, errors='ignore') as f >>> # did not look up signature, >>> for line in f: >>> do_stuff(line) >> >> >> The above is fine but... >> >>> To make this reusable, wrap in 'def filelines(files):' and replace >> >> 'do_stuff(line)' with 'yield line'. >> >> That doesn't work entirely correctly as you end up yielding from inside a >> with statement. If the user of your generator function doesn't fully >> consume the generator then whichever file is currently open is not >> guaranteed to be closed. > > > You can convert the generator to context manager and use it in the with statement to guarantee closing. > > with contextlib.closing(filelines(files)) as f: > for line in f: > ... Or you can use fileinput which is designed to be exactly this kind of context manager and to be used in this way. Although fileinput is slightly awkward in defaulting to reading stdin. -- Oscar