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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:06:17+00:00 2026-05-26T15:06:17+00:00

I have several files, and I need to replace third line in them: files

  • 0

I have several files, and I need to replace third line in them:

files = ['file1.txt', 'file2.txt']
new_3rd_line = 'new third line'

What is the best way to do this?

Files are big enough, several 100mb’s files.

  • 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-26T15:06:18+00:00Added an answer on May 26, 2026 at 3:06 pm

    I used this solution: Search and replace a line in a file in Python

    from tempfile import mkstemp
    from shutil import move
    from os import remove, close
    
    def replace_3_line(file):
        new_3rd_line = 'new_3_line\n'
        #Create temp file
        fh, abs_path = mkstemp()
        new_file = open(abs_path,'w')
        old_file = open(file)
        counter = 0
        for line in old_file:
            counter = counter + 1
            if counter == 3:
                new_file.write(new_3rd_line)
            else:
                new_file.write(line)
        #close temp file
        new_file.close()
        close(fh)
        old_file.close()
        #Remove original file
        remove(file)
        #Move new file
        move(abs_path, file)
    
    replace_3_line('tmp.ann')
    

    But it does not work with files that contains non English charecters.

    Traceback (most recent call last):
      File "D:\xxx\replace.py", line 27, in <module>
        replace_3_line('tmp.ann')
      File "D:\xxx\replace.py", line 12, in replace_3_line
        for line in old_file:
      File "C:\Python31\lib\encodings\cp1251.py", line 23, in decode
        return codecs.charmap_decode(input,self.errors,decoding_table)[0]
    UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 32: character maps to <undefined>
    

    That is bad. Where’s python unicode? (file is utf8, python3).

    File is:

    фвыафыв
    sdadf
    试试
    阿斯达а
    阿斯顿飞
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several sql files. I need to execute them in a transaction. When
I have several large csv files with thousands of columns that I need to
I have several files containing this line Release: X I want to increment X
I have several log files of events (one event per line). The logs can
I'm trying to perform a search-replace in several JCLs but I need multi-line capabilities,
I have several (old) Drupal-sites that need to be replaced with one single new
I have several nib files and I need to track how many times the
I have several files comprising a tomcat web application that I need to import
I have several 50+ MB sized csv files I need to import to my
I have several XML files used in my application, I need to document these

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.