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

  • SEARCH
  • Home
  • 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 9221523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:35:53+00:00 2026-06-18T03:35:53+00:00

I have a text file (file1.txt) with 1 string per line over many lines.

  • 0

I have a text file (file1.txt) with 1 string per line over many lines.
I’m trying to read in the file and write certain lines to a new file.(file2.txt)

My text file looks something like this.

foo1
foo2
foo3
foo4
foo5
foo6

etc..

for example, i want to write foo1,foo2,foo4,foo6 to my new file and miss out foo3 and foo5.

foo1
foo2
foo4
foo6

I wish to preserve the original file.

My code looks like this…

with open("file1.txt","r") as r:
    lines=r.read()
    lines =lines.replace("foo3","")
    lines = lines.replace("foo5","")

r.close()
with open("file2.txt","a") as w:
    w.write(lines)
w.close

The problem is I end up with this output..

foo1
foo2

foo4

foo6

I think this is because i am replacing foo with “”
how do I get rid of the white space?

TIA,

Paul.

  • 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-06-18T03:35:54+00:00Added an answer on June 18, 2026 at 3:35 am

    The minimal change is to also replace the line separators by changing the replace calls to:

    lines =lines.replace("foo3\n","")
    lines = lines.replace("foo5\n","")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two text file. file1.txt has: gedit google chrome git vim foo bar
I have a text file (txt) containing formatted text (just line breaks, carriage returns
I have a text file named num.txt who's only contents is the line 123
I have the first few lines of a text file fx.txt with the following
I have a .txt file having some text. I want to read this file
I have a text file named test.txt I want to write a C program
A text file I what to read has a certain pattern: Name line [tag
From GWT I read a text file myFile.txt as per below. The issue is
I have a text file(txt) in my web server. I will read the file
Hello I have a text file which I am trying to read a set

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.