r/pythonhelp • u/GeneralIrohLovesZuko • Jul 03 '22
How do I delete lines consecutively in a text file?
I have been trying to make a password saver for a generator, and I want to delete lines consecutively in the file. Here is some of my code:
if occ >= 1:rewrite = input("Would you like to rewrite the password for:"+ reason+ "?\n")if rewrite == "yes":f = open(reason, "a+")f.truncate(0)f.close()os.remove(f)shutil.move('C:/Python Practice/Password-Generator-30-01-22/'+reason, 'C:/Python Practice/Password-Generator-30-01-22/Password file folder and generator')`
1
u/PicoPlanetDev Jul 03 '22
I'm wondering if you could share more of the code or logic that it uses because I'm not sure just from this what the program is trying to accomplish.
Also, RTF is generally pretty cluttered. If possible I'd suggest switching to plaintext.
1
u/Goobyalus Jul 05 '22 edited Jul 05 '22
Alsonext(file) doesn't work here, the for loop has its own iterator over the file that isn't affected by your nexts.
1
u/Goobyalus Jul 05 '22
I think this is what OP's code looks like formatted properly:
doesn't do anything
will overwrite
Things2Keepwith a new string each time instead of appending to it