I have a file that contains:
Line_1
Line_2
Line_3
Line_4
I want to delete the last line of the file Line_4
while opening the file, NOT using python list methods, and as follwoing:
with open('file.txt', 'r+') as f:
lines = f.readlines()
if len(lines) > 3:
f.seek(0)
for i in lines:
if i != 4:
f.write(i)
f.truncate()
The above solution is not working. I also have used the os.SEEK_END
as follwoing:
with open('file.txt', 'r+') as f:
lines = f.readlines()
if len(lines) > 3:
f.seek(0, os.SEEK_END)
f.truncate()
But, it is not working as well !