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
#1
Apologies in advance for something that I'm sure you all have encountered before - but I cant find an answer!

The following code happily writes each line as a newline in the output file, unless the difference found is on the last line(I'm using difflib.Differ).
Hope you can see but the final '? ^' of each input file should be on a newline. I did try adding a newline (\n) to my write statement, but that just inserted every other line as spaces in the output

I'm sure there are many other ways to do this, but I'm just looking to understand what is the cause of the issue and the resolution - I suppose I could test if its the last record in the file and treat it differently, but that seems a bit rubbish.

for line in difference.compare(a, b):
   f3.write(line)  
- dhx22
? ^
+ dhj22
? ^
- d23y
? -

- aaaad34? ^
+ aabad34? ^
Reply
#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
#3
Thanks for getting back to me @deanhystad. I think your hunch must be correct as your revised code works as expected.
Reply
#4
From the difflib documentation:
Quote:compare(a, b)
Compare two sequences of lines, and generate the delta (a sequence of lines).

Each sequence must contain individual single-line strings ending with newlines. Such sequences can be obtained from the readlines() method of file-like objects. The delta generated also consists of newline-terminated strings, ready to be printed as-is via the writelines() method of a file-like object.
It may be slightly inconvenient, but at least it tells you up front.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  If I open a file write or append, is the file loaded into RAM? Pedroski55 11 1,117 Jan-14-2026, 07:49 AM
Last Post: Pedroski55
  how to write/overwrite data in a txt. file according to inp Quinn 2 1,586 Aug-12-2025, 04:20 PM
Last Post: Quinn
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 13 36,670 May-20-2025, 12:26 PM
Last Post: hanmen9527
  How to write variable in a python file then import it in another python file? tatahuft 4 2,252 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  [SOLVED] [Linux] Write file and change owner? Winfried 6 3,192 Oct-17-2024, 01:15 AM
Last Post: Winfried
  how to process 12 million record pymach 4 2,009 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 2,259 Apr-22-2024, 01:15 PM
Last Post: snippsat
  write to csv file problem jacksfrustration 11 8,640 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 5,857 Nov-09-2023, 10:56 AM
Last Post: mg24
  Facing issue in python regex newline match Shr 6 10,374 Oct-25-2023, 09:42 AM
Last Post: Shr

Forum Jump:

User Panel Messages

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