[Tutor] reading file, adding to each line, writing file

David ldl08 at gmx.net
Wed Feb 4 15:50:47 CET 2009


Hello Albert,

thanks for your answer!

A.T.Hofkamp wrote:
>
> The 'while' loop can be replaced by a 'for' loop, like
>
> for line in infile:
>   outfile.write(line)

I first was thinking of/experimenting with a 'for' loop, but what I 
originally had in mind was to combine the 'for' loop with readline() -- 
which Python disallows. So I changed to a 'while' loop.

> The simplest solution would be to construct a new line from the old 
> one directly below the 'while', for example
>
> line2 = line[:-1] + " -d\n"
>
> followed by writing line2 to disk.
>
>
> The question that arises however is, what should be done with a line like
>
> "bla bla                 \n"
>
> Do you want
>
> "bla bla                 -d\n"
>
> or do you want
>
> "bla bla -d\n"
>
> here? 
I want "bla bla" -d\n ;-)

Let me try your suggestions out!

Cheers,

David


More information about the Tutor mailing list