Rewrite files in Python -


i having issues writing on textfiles in python. change open('tag.txt', 'r') open('tag.txt', 'w') file gets empty. in code below have managed manipulate want print textfile can not write in actual file can print 1 in code instead. need somehow create code when "if columns[4] > localtime[3]:" rewrite textfile new information not need print columns can print textfile instead.

def alla_tag():     localtime = time.asctime(time.localtime(time.time()))     localtime = localtime.split(' ')     tagfil = open('tag.txt', 'r')     line in open('tag.txt'):         columns = line.split()         if columns[4] > localtime[3]:             print(columns[0] + ' ' + columns[1] + ' ' + columns[2] + ' ' + columns[3]  + ' ' + columns[4])     tagfil.close() 

help someone? (beginner python)

you need open file in append mode using open(file_name, 'a'). need edit line of code opening file as:

tagfil = open('tag.txt', 'a') 

if open file in w mode stands writable mode, every time previous contents of file omitted , new contents override previous saved content, continue editing previous file contents need open file in append mode using a flag in open() function.


Comments