Split single file into multiple files based on patterns
demianbrecht at gmail.com
Wed Oct 24 07:36:56 CEST 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:
for count, file_data in enumerate(turn_text_to_txt):
> f = open('/home/david/files/%s_%s.txt' % (file_data.split(' '), count), 'w')
with open('file path', 'w') as f:
Not only is it shorter, but it automatically closes the file once you've come out of the inner block, whether successfully or erroneously.
More information about the Python-list