[Tutor] files question

Drew Perttula drewp@bigasterisk.com
Thu Jan 23 17:09:24 2003


> This keeps adding to the list each time  run it. This is what I get
> =

> [['1', '2', '3'], ['one', 'two', 'three']]
> [['1', '2', '3', ''], ['one', 'two', 'three', '']]
> [['1', '2', '3', '', ''], ['one', 'two', 'three', '', '']]
> =



> # WRITE BACK TO FILE
> def saveme():
> =A0=A0=A0 data =3D open('testing.txt', 'w')
> =A0=A0=A0 for x in range(len(hold)):
---> =A0=A0=A0=A0=A0=A0=A0 hold[x].append('\n')
> =A0=A0=A0=A0=A0=A0=A0 join_up =3D string.join(hold[x], '\t')
> =A0=A0=A0=A0=A0=A0=A0 data.write(join_up)
> =A0=A0=A0 data.close()

The indicated line is adding the newline to the list, which means it'll
get a tab to "separate" it from the previous list elements. Instead,
remove the indicated line and make the next one something like:

   join_up =3D string.join(hold[x], '\t')+"\n"

Also, I fixed "data.close" (get the close method and do nothing with it-
a legal no-op) to "data.close()" (call the close method), although that
won't affect the behavior of this little program.

-Drew