Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:file': 0.07; "'w')": 0.09; 'subject:files': 0.09; 'subject:into': 0.09; 'url:github': 0.09; 'cc:addr:python-list': 0.10; 'closes': 0.16; 'count,': 0.16; 'count.': 0.16; 'subject:based': 0.16; 'wrote:': 0.17; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'skip:@ 10': 0.27; 'block,': 0.29; 'file': 0.32; 'received:209.85.160.46': 0.32; 'received:google.com': 0.34; 'pm,': 0.35; 'received:192.168.0': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'message-id:@gmail.com': 0.36; 'charset:us-ascii': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; 'header:Received:5': 0.40; "you've": 0.61; 'header:Message-Id:1': 0.62 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; bh=sEAEIqbiWTot2akw3X/u2+sl4j/udZUQSBV2Ekj2OvQ=; b=zLNkE58owUtgEAxV3sTvN7U74wqC8UV0eQIQj5hSYFSqMzKdAGIXBbLUqgwDBk4mhS CT+++R2Cagl/hKuUEl4vjdoB2Zp1wzJhqh8fQe9vHAJQ/WMVn+j1ATruQcxBDE2Y+bhJ sgbddX1fJX2v/Z3VfK4YtmF+eYlG/H6067pn5DKmnYCxmosf0lVmPYV4zJNhS0OSTU24 1lIBHrQ61n6tQFfKD4jqt6uQs42EHOHG/UqYTYteu+wYH9+WblruDcoh31V2RWkl4Cjz 0mOxOsGRpY4fl4J9HjpEzuK4qTfxyXq8ZuRkqoxfuIp3eCCysnRhECItb2drAF2PRPgP 44Fg== Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 6.0 \(1485\)) Subject: Re: Split single file into multiple files based on patterns From: Demian Brecht In-Reply-To: Date: Tue, 23 Oct 2012 22:36:56 -0700 Content-Transfer-Encoding: quoted-printable References: <9ae3b43a-5c69-4232-a1cf-e2dd0c4ee2ca@googlegroups.com> To: David Hutto X-Mailer: Apple Mail (2.1485) Cc: python-list@python.org 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351057021 news.xs4all.nl 6933 [2001:888:2000:d::a6]:33415 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31995 On 2012-10-23, at 10:24 PM, David Hutto wrote: > count =3D 0 Don't use count. > for file_data in turn_text_to_txt: Use enumerate:=20 for count, file_data in enumerate(turn_text_to_txt): > f =3D open('/home/david/files/%s_%s.txt' % (file_data.split(' ')[0], = count), 'w') Use with:=20 with open('file path', 'w') as f: f.write('data') Not only is it shorter, but it automatically closes the file once you've = come out of the inner block, whether successfully or erroneously. Demian Brecht @demianbrecht http://demianbrecht.github.com