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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:40:58+00:00 2026-06-15T17:40:58+00:00

Possible Duplicate: Search and replace a line in a file in Python How do

  • 0

Possible Duplicate:
Search and replace a line in a file in Python
How do I modify a text file in Python?

I have an input file that I need to rewrite with the different files needed to be modified before running a program. I have tried a variety of the solutions on here but none of them seem to work. I end up just overwriting my file with a blank file

f = open(filename, 'r+')
text = f.read()
text = re.sub('foobar', 'bar', text)
f.seek(0)
f.write(text)
f.truncate()
f.close()

Or with that code for instance the name I am changing is different each time I run the program so I need to replace the entire line not just one keyword

  • 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-15T17:40:59+00:00Added an answer on June 15, 2026 at 5:40 pm

    A simple way may be to read the text into a string, then concatenate the string with the text you want to write:

    infile = open('hey.txt','r+')
    content = infile.read()
    text = ['foo','bar']
    for item in text:
         content +=item  #adds 'foo' on first iteration, 'bar' on second
    infile.write(content)
    infile.close()
    

    or to change a particular key word:

    infile = open('hey.txt','r+')
    content = infile.read()
    table = str.maketrans('foo','bar')
    content = content.translate(table)  #replaces 'foo' with 'bar'
    infile.write(content)
    infile.close()
    

    or to change by line, you can use readlines and refer to each line as the index of a list:

    infile = open('hey.txt','r+')
    content = infile.readlines() #reads line by line and out puts a list of each line
    content[1] = 'This is a new line\n' #replaces content of the 2nd line (index 1)
    infile.write(content)
    infile.close()
    

    Maybe not a particularly elegant way to solve the problem, but it could be wrapped up in a function and the ‘text’ variable could be a number of data types like a dictionary, list, etc. There are also a number of ways to replace each line in a file, it just depends on what the criteria are for changing the line (are you searching for a character or word in the line? Are you just looking to replace a line based on where it is in the file?)–so those are also some things to consider.

    Edit: Added quotes to third code sample

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

Sidebar

Related Questions

Possible Duplicate: Bind textbox to 'enter' key I have a search text box. I
Possible Duplicate: Search and Replace Entire Project (Netbeans) Other IDE's have a feature to
Possible Duplicate: Sed/Awk to search and replace/insert text in files I would like to
Possible Duplicate: Search and replace inside an associative array I think this may have
Possible Duplicate: MySQL full text search with partial words I have a search feature
Possible Duplicate: Need help in building efficient exhaustive search algorithm Imagine that you must
Possible Duplicate: Search-like highlighting in NSTextView I thought i read here months ago that
Possible Duplicate: Do most browsers automatically search for the favicon.ico? Will a favicon.ico that
Possible Duplicate: Windows Phone 7 Search Button I think that all Windows Phone mobiles
Possible Duplicate: Choosing a stand-alone full-text search server: Sphinx or SOLR? I will use

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.