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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:39:59+00:00 2026-05-12T06:39:59+00:00

I want to read a line in a file and insert the new line

  • 0

I want to read a line in a file and insert the new line (“\n”) character in the n position on a line, so that a 9-character line, for instance, gets converted into three 3-character lines, like this:

"123456789" (before)
"123\n456\n789" (after)

I’ve tried with this:

f = open(file, "r+")
f.write("123456789")
f.seek(3, 0)
f.write("\n")
f.seek(0)
f.read()

-> ‘123\n56789’

I want it not to substitute the character in position n, but only to insert another (“\n”) char in that position.

Any idea about how to do this?
Thanks

  • 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-12T06:40:00+00:00Added an answer on May 12, 2026 at 6:40 am

    I don’t think there is any way to do that in the way you are trying to: you would have to read in to the end of the file from the position you want to insert, then write your new character at the position you wish it to be, then write the original data back after it. This is the same way things would work in C or any language with a seek() type API.

    Alternatively, read the file into a string, then use list methods to insert your data.

    source_file = open("myfile", "r")
    file_data = list(source_file.read())
    source_file.close()
    file_data.insert(position, data)
    open("myfile", "wb").write(file_data)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file that I want to read line by line and
I want to go to line 34 in a .txt file and read it.
I want to write a function that read line by line from a socket
I have a text file. I want read that file. But In that if
I have a Java program that reads lines from text file. I want to
I want to run an SQL file that has a lot of Insert Statments,
So I've developed a system that saves database insert/update commands into a text file
I want to read a specific line from an html source code. Im storing
I want to read a specific line of a RSS feed to display the
When I want to add the line SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED right

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.