Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6104735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:52:33+00:00 2026-05-23T13:52:33+00:00

The difflib.context_diff method returns a generator, showing you the different lines of 2 compared

  • 0

The difflib.context_diff method returns a generator, showing you the different lines of 2 compared strings. How can I write the result (the comparison), to a text file?

In this example code, I want everything from line 4 to the end in the text file.

>>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
>>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
>>> for line in context_diff(s1, s2, fromfile='before.py', tofile='after.py'):
...     sys.stdout.write(line)  # doctest: +NORMALIZE_WHITESPACE
*** before.py
--- after.py
***************
*** 1,4 ****
! bacon
! eggs
! ham
  guido
--- 1,4 ----
! python
! eggy
! hamster
  guido

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:52:33+00:00Added an answer on May 23, 2026 at 1:52 pm
    with open(..., "w") as output:
        diff = context_diff(...)
        output.writelines(diff)
    

    See the documentation for file.writelines().

    Explanation:

    1. with is a context manager: it handles closing the file when you are done. It’s not necessary but is good practice — you could just as well do

      output = open(..., "w")
      

      and then either call output.close() or let Python do it for you (when output is collected by the memory manager).

    2. The "w" means that you are opening the file in write mode, as opposed to "r" (read, the default). There are various other options you can put here (+ for append, b for binary iirc).

    3. writelines takes any iterable of strings and writes them to the file object, one at a time. This is the same as for line in diff: output.write(line) but neater because the iteration is implicit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the difflib.HtmlDiff class, calling the function using two sets of text
I am trying to use Difflib.SequenceMatcher to compute the similarities between two files. These
After finding the difflib.SequenceMatcher class in Python's standard library to be unsuitable for my
After finding the difflib.SequenceMatcher class in Python's standard library to be unsuitable for my
I'd like to align two lists in a similar way to what difflib.Differ would
I want to generate a diff between to revisions of text (more specifically, Markdown-formatted
I am using difflib to compare files in two directories (versions from consecutive years).
I have been using the Python difflib library to find where 2 documents differ.
I have two chunks of text that I would like to compare and see
I'm trying to diff two strings to determine whether or not they solely vary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.