[Tutor] files question

Ron A clickron@webtv.net
Thu Jan 23 16:35:02 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', '', '']]

I've been playing with it and it's driving me nuts. I'm sure there are
better ways to do this, but I gotta know what I've done wrong. Any help
is appreciated.

hold =3D []
import string
=A0
# OPEN AND READ A FILE
def mylist():
=A0=A0=A0 data =3D open('testing.txt', 'r')
=A0=A0=A0 while 1:
=A0=A0=A0=A0=A0=A0=A0 read_me =3D data.readline()
=A0=A0=A0=A0=A0=A0=A0 if read_me =3D=3D '':
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 break
=A0=A0=A0=A0=A0=A0=A0 no_n =3D read_me[:-1]
=A0=A0=A0=A0=A0=A0=A0 split_me =3D string.split(no_n, '\t')
=A0=A0=A0=A0=A0=A0=A0 hold.append(split_me)
=A0=A0=A0 print hold
=A0=A0=A0 data.close()
=A0
# 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
=A0
mylist()
saveme()

Ron A=A0