from os import unlink from os.path import exists if exists('sample.txt'): unlink('sample.txt') f = open('sample.txt','ab+') f.write('this is a test\n') f.seek(0) print f.read()