[Tutor] Iterating Lines in File and Export Results

Alan Gauld alan.gauld at btinternet.com
Fri Oct 3 00:04:00 CEST 2014


On 02/10/14 16:47, John Doe wrote:

> def loop_extract():
>      with open('words.txt', 'r') as f:
>          for lines in f:
>              #print lines (I confirmed that each line is successfully printed)
>              with open('export.txt', 'w') as outf:

This opens and closes the file for each iteration of the inner loop.
You need this outside the loop beside the other with statement.

>                  outf.write(lines)

HTH


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list