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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:07:59+00:00 2026-06-05T23:07:59+00:00

I am having a problem with a file i am writing too, basically what

  • 0

I am having a problem with a file i am writing too, basically what i want it to do is, i got a input() saying “enter your name: ” stored in a variable, now i created the file wrote what i wanted and based on what the person has input() i wanna place that information into the file on a specific line…

This is what i did to do it and it works, but it is overwriting current text rather then inserting the text and shifting the current text over…

eg;

index = open('index.html' 'w')
index.write(""" blah blah blah
                blah blah blah
                blah """)
index.seek(20)
index.write(variable)
index.close()

now all i want the variable to do is go into the file like it is doing but not overwrite current text, any advice would be appreciated.

I am using Python3.2.

  • 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-05T23:08:01+00:00Added an answer on June 5, 2026 at 11:08 pm

    To insert text into a file, read in the text from the file into a variable. Insert the text in the correct place, and then write the text back to the file.

    Like so:

    with open("filename", 'rt', encoding="utf8") as infile:
        text = infile.read()
    
    text = text[:20] + 'inserted text' + text[20:]
    
    
    with open("filename", 'wt', encoding="utf8") as outfile:
        outfile.write(text)
    

    However, from your question, I suspect that the file in fact does not exist beforehand, in which case the best way is to simply create the whole text as a variable first, and then write it to file.

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

Sidebar

Related Questions

I'm having a problem with arrays and file writing, what I want to do
I'm having a problem writing to a file: FileInputStream fin; try { fin =
Hi I am having no problem writing to or appending to a file, the
I'm having a major problem with writing values into a new CSV file. I
I'm writing a simple ini file parser and I'm having a little problem with
I'm having a problem writing Norwegian characters into an XML file using C#. I
i'm having problem writing to a text file using php. this might sound simple
I am having some problems writing to a file in unicode inside my c
I have a flash file menu i am having problem while link it to
I'm having a problem parsing a JSON file in AS3. Im trying to parse

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.