fd=open("file.txt","r") d=fd.read() fd.close() m=d.split("\n") s="\n".join(m[:-1]) fd=open("file.txt","w+") for i in range(len(s)): fd.write(s[i]) fd.close()
Here is what the above code is Doing:
1. Open the file in read mode.
2. Read the file and store it in a variable.
3. Split the variable on the basis of newline character.
4. Join the list of strings except the last one.
5. Open the file in write mode.
6. Write the string to the file.
7. Close the file.