the scope of f.write('') was not clear, modified into f.truncate(0)