Why doesn’t ‘write’ overwrites an open file everytime a loop iterates?

  python, python-3.x

For the following code in Python:

L = ['vanilla', 6, 'chocola', 7]
txt = 'empty.txt'
f = open(txt,'w')
for item in L:
    f.write(str(item))
f.close()
f = open('empty.txt','r')
print(f.readlines())
f.close()

I ran it in Python and ‘vanilla6chocola7’ was printed
But shouldn’t it print just ‘7’?

Doesn’t ‘write’ overwrites everything in the file?

Source: Python-3x Questions

LEAVE A COMMENT