Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31995
| Subject | Re: Split single file into multiple files based on patterns |
|---|---|
| From | Demian Brecht <demianbrecht@gmail.com> |
| Date | 2012-10-23 22:36 -0700 |
| References | <9ae3b43a-5c69-4232-a1cf-e2dd0c4ee2ca@googlegroups.com> <CA+vVgJUx5sVHZnyTzqrQqYwa+8+ZFb-80oxv7LsmwF=Hs6xqhw@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2719.1351057021.27098.python-list@python.org> (permalink) |
On 2012-10-23, at 10:24 PM, David Hutto <dwightdhutto@gmail.com> wrote:
> count = 0
Don't use count.
> for file_data in turn_text_to_txt:
Use enumerate:
for count, file_data in enumerate(turn_text_to_txt):
> f = open('/home/david/files/%s_%s.txt' % (file_data.split(' ')[0], count), 'w')
Use with:
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Split single file into multiple files based on patterns satyam <dirac.sat@gmail.com> - 2012-10-23 20:01 -0700
Re: Split single file into multiple files based on patterns Jason Friedman <jason@powerpull.net> - 2012-10-23 21:36 -0600
Re: Split single file into multiple files based on patterns Jason Friedman <jason@powerpull.net> - 2012-10-23 21:43 -0600
Re: Split single file into multiple files based on patterns David Hutto <dwightdhutto@gmail.com> - 2012-10-24 01:24 -0400
Re: Split single file into multiple files based on patterns Demian Brecht <demianbrecht@gmail.com> - 2012-10-23 22:36 -0700
Re: Split single file into multiple files based on patterns Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-24 01:38 -0400
Re: Split single file into multiple files based on patterns Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-10-24 07:46 +0200
Re: Split single file into multiple files based on patterns Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-24 07:13 +0100
Re: Split single file into multiple files based on patterns Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-24 07:52 +0000
Re: Split single file into multiple files based on patterns David Hutto <dwightdhutto@gmail.com> - 2012-10-24 04:02 -0400
Re: Split single file into multiple files based on patterns Peter Otten <__peter__@web.de> - 2012-10-24 10:17 +0200
csiph-web