[Solved] does (‘filename’,’w’) replace or add [closed]


f = open('file','w')
f.write('hi'+'\n')
f.close()

output: “hi” in file nothing else, this is because ‘w’ will overwrite the file causing it to be blank before entering the text,

f = open('file','a')
f.write('hi'+'\n')
f.close()

output whatever’s in text file to begin with + “Hi”, this will add the text to the file

f = open('file','r')
f.write('hi'+'\n')
f.close()

output error since you opened the file to read and therefore you cant add text

solved does (‘filename’,’w’) replace or add [closed]