[2.5.1] Read each line from txt file, replace, and save?

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Sep 2 07:02:02 EDT 2012


On 02/09/2012 11:36, Gilles wrote:
> On Sun, 02 Sep 2012 12:19:02 +0200, Gilles <nospam at nospam.com> wrote:
> (snip)
>
> Found it:
>
> #rewrite lines to new file
> output = open('output.txt','w')
>
> for line in textlines:
> 	#edit each line
> 	line = "just a test"
> 	output.write("%s" % line)
>
> output.close()
>

IMHO better practice to use the with statement.  See 
http://docs.python.org/reference/compound_stmts.html#the-with-statement

-- 
Cheers.

Mark Lawrence.




More information about the Python-list mailing list