open file and rewrite it with new contents