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

Andre Engels andreengels at gmail.com
Wed Feb 4 15:54:04 CET 2009


On Wed, Feb 4, 2009 at 3:30 PM, David <ldl08 at gmx.net> wrote:
> Hello everybody,
>
> I have easily spent some four hours on this problem, and I am now asking for
> rescue.
>
> Here is what I am trying to do: I have a file ("step2", with some 30 or so
> lines. To each line I would like to add " -d" at the end. Finally, I want to
> save the file under another name ("pyout".
> So far I have managed to read the file, line by line, and save it under
> another name:
>
> <code>
>
> # add " -d" to each line of a textfile
>
> infile = open("step2", 'r') # open file for appending
> outfile = open("pyout","a") # open file for appending
>
> line = infile.readline()    # Invokes readline() method on file
> while line:
>    outfile.write(line),    # trailing ',' omits newline character
>    line = infile.readline()
>
> infile.close()
> outfile.close()
>
> </code>
>
> As I said, before writing to file "pyout" I would like to append the string
> " -d" to each line. But how, where? I can't append to strings (which the
> lines gained with infile.readline() seem be), and my trial and error
> approach has brought me nothing but a headache.

You cannot append to strings, but you can add to them!

So the first step would be:

> # add " -d" to each line of a textfile
infile = open("step2", 'r') # open file for reading
outfile = open("pyout","a") # open file for appending

line = infile.readline()    # Invokes readline() method on file
while line:
   outfile.write(line+" -d"),    # trailing ',' omits newline character
   line = infile.readline()

infile.close()
outfile.close()


However, that doesn't work fine, because the newline is at the end of
the line, and thus the -d is coming at the beginning. To work that
out, we use string.strip() which allows us to remove certain
characters from the beginning and end of a string:

# add " -d" to each line of a textfile

infile = open("step2", 'r') # open file for reading
outfile = open("pyout","a") # open file for appending

line = infile.readline()    # Invokes readline() method on file
while line:
  outfile.write(line.strip("\n")+" -d\n")
  line = infile.readline()

infile.close()
outfile.close()


By the way, a somewhat more elegant method (in my opinion at least) is
to use readlines(), which gives a generator of the lines in a file,
rather than readline():

# add " -d" to each line of a textfile

infile = open("step2", 'r') # open file for appending
outfile = open("pyout","a") # open file for appending

for line in infile.readlines():
  outfile.write(line.strip("\n")+" -k\n")

infile.close()
outfile.close()


Yet another method would be a combination of read and replace:

# add " -d" to each line of a textfile

infile = open("step2", 'r') # open file for appending
outfile = open("pyout","a") # open file for appending

outfile.write(infile.read().replace("\n"," -d\n"))

infile.close()
outfile.close()


However, this is sensitive to the presence or absence of a newline
after the last line.


--
André Engels, andreengels at gmail.com


More information about the Tutor mailing list