Split single file into multiple files based on patterns

Demian Brecht demianbrecht at gmail.com
Wed Oct 24 01:36:56 EDT 2012


On 2012-10-23, at 10:24 PM, David Hutto <dwightdhutto at 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







More information about the Python-list mailing list