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 6235463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:40:21+00:00 2026-05-24T10:40:21+00:00

I am trying to write a function in Python 3 that will write all

  • 0

I am trying to write a function in Python 3 that will write all lines that end with the string ‘halloween’ to a file. When I call this function, I can only get one line to write to the output file (file_2.txt). Can anyone point out where my problem is? Thanks in advance.

def parser(reader_o, infile_object, outfile_object):
    for line in reader_o:
        if line.endswith('halloween'):
            return(line)

with open("file_1.txt", "r") as file_input:
    reader = file_input.readlines()
    with open("file_2.txt", "w") as file_output:
        file_output.write(parser(reader))
  • 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-24T10:40:22+00:00Added an answer on May 24, 2026 at 10:40 am
    def parser(reader_o):
        for line in reader_o:
            if line.rstrip().endswith('halloween'):
                yield line
    
    with open("file_1.txt", "r") as file_input:
        with open("file_2.txt", "w") as file_output:
            file_output.writelines(parser(file_input))
    

    This is called a generator. It can also be written as an expression instead of a function:

    with open("file_1.txt", "r") as file_input:
        with open("file_2.txt", "w") as file_output:
            file_output.writelines(line for line in file_input if line.rstrip().endswith('halloween'))
    

    If you’re on Python 2.7 / 3.2, you can do the two withs like this:

    with open("file_1.txt", "r") as file_input, open("file_2.txt", "w") as file_output:
    

    You don’t need to do readlines() on the file, just telling the loop to iterate over the open file itself will do the exact same thing.

    Your problem was that return always would exit the loop on the first match. yield stops the loop, passes out the value, then the generator can be started again from the same point.

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

Sidebar

Related Questions

I'm trying to write a Python script that will get the md5sum of all
I am trying to write my first real python function that does something real.
I'm trying to write a function that formats every (string) member/variable in an object,
I am trying to write a function that will pull the name of a
I'm trying to write a function that is able to determine whether a string
I'm trying to write a regex function that will identify and replace a single
I am trying to write a JavaScript function that will return its first argument(function)
I'm trying to write a custom function that will let me retrieve a cell
I am trying to write a function that will take an xml object, an
I am trying to write some code that will function like google calendars quick

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.