overwrite

Delete the contents of a file before writing to it (in Python)?

浪尽此生 提交于 2020-07-15 05:07:51
问题 I'm trying my hand at this rosalind problem and am running into an issue. I believe everything in my code is correct but it obviously isn't as it's not running as intended. i want to delete the contents of the file and then write some text to that file. The program writes the text that I want it to, but it doesn't first delete the initial contents. def ini5(file): raw = open(file, "r+") raw2 = (raw.read()).split("\n") clean = raw2[1::2] raw.truncate() for line in clean: raw.write(line) print

Delete the contents of a file before writing to it (in Python)?

感情迁移 提交于 2020-07-15 05:05:18
问题 I'm trying my hand at this rosalind problem and am running into an issue. I believe everything in my code is correct but it obviously isn't as it's not running as intended. i want to delete the contents of the file and then write some text to that file. The program writes the text that I want it to, but it doesn't first delete the initial contents. def ini5(file): raw = open(file, "r+") raw2 = (raw.read()).split("\n") clean = raw2[1::2] raw.truncate() for line in clean: raw.write(line) print