Apr-02-2019, 02:37 AM
You open the file as mode 'a' which means append.
It will create the file if it doesn't exist, but if it does exist, it will retain contents and append new contents.
you should open as mode 'w' if you don't want this.
It will create the file if it doesn't exist, but if it does exist, it will retain contents and append new contents.
you should open as mode 'w' if you don't want this.
def write_file(filename, intval): with open(filename, 'w') as fp: print(f'intval: {intval}') fp.write(str(intval)) def read_file(filename): with open(filename) as fp: return fp.read() if __name__ == '__main__': myfile = 'tryit.txt' CurrentCount = 300 write_file(myfile, CurrentCount) print(f'reading result: {read_file(myfile)}')output:
Output:intval: 300
reading result: 300
hexdump:Output:$ hd tryit.txt
00000000 33 30 30
00000003