Python Forum
Last record in file doesn't write to newline
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Last record in file doesn't write to newline
#2
I don't have the problem you describe using difflib. Could you provide a little more context please. What are a, b and difference? Could you post a complete example?

My guess is that a and b are strings read from two files. All the lines end with a newline except the last. If that's the problem, you can hardly fault difflib that your input files don't end with an empty line.

You could do something like this, letting you control where newlines appear in the output file:
from difflib import Differ

with open("text.txt", "w") as file, open("a.txt", "r") as a, open("b.txt", "r") as b:
    for x in Differ().compare(a.readlines(), b.readlines()):
        print(x.strip(), file=file)
Reply


Messages In This Thread
RE: Last record in file doesn't write to newline - by deanhystad - Jan-21-2024, 11:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 27,879 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  How to write variable in a python file then import it in another python file? tatahuft 4 875 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,489 Oct-17-2024, 01:15 AM
Last Post: Winfried
  how to process 12 million record pymach 4 1,075 Aug-01-2024, 07:58 AM
Last Post: Pedroski55
  What does .flush do? How can I change this to write to the file? Pedroski55 3 1,305 Apr-22-2024, 01:15 PM
Last Post: snippsat
  write to csv file problem jacksfrustration 11 4,862 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,749 Nov-09-2023, 10:56 AM
Last Post: mg24
  Facing issue in python regex newline match Shr 6 5,918 Oct-25-2023, 09:42 AM
Last Post: Shr
  How do I stream and record at the same time with arducam? traderjoe 0 1,030 Oct-23-2023, 12:01 AM
Last Post: traderjoe
  How do I read and write a binary file in Python? blackears 6 24,371 Jun-06-2023, 06:37 PM
Last Post: rajeshgk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020