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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:12:25+00:00 2026-06-13T20:12:25+00:00

Possible Duplicate: Fastest Way to Delete a Line from Large File in Python How

  • 0

Possible Duplicate:
Fastest Way to Delete a Line from Large File in Python
How to edit a line in middle of txt file without overwriting everything?

I know I can read every line into a list, remove a line, then write the list back.

But the file is large, is there a way to remove a part in the middle of the file, and needn’t rewrite the whole file?

  • 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-13T20:12:26+00:00Added an answer on June 13, 2026 at 8:12 pm

    I don’t know if a way to change the file in place, even using low-level file system commands, but you don’t need to load it into a list, so you can do this without a large memory footprint:

    with open('input_file', 'r') as input_file:
        with open('output_file', 'w') as output_file:
            for line in input_file:
                if should_delete(line):
                    pass
                else:
                    output_file.write(line)
    

    This assumes that the section you want to delete is a line in a text file, and that should_delete is a function which determines whether the line should be kept or deleted. It is easy to change this slightly to work with a binary file instead, or to use a counter instead of a function.

    Edit: If you’re dealing with a binary file, you know the exact position you want to remove, and its not too near the start of the file, you may be able to optimise it slightly with io.IOBase.truncate (see http://docs.python.org/2/library/io.html#io.IOBase). However, I would only suggest pursuing this if the a profiler indicates that you really need to optimise to this extent.

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

Sidebar

Related Questions

Possible Duplicate: Fastest way to list all primes below N in python Although I
Possible Duplicate: What is the fastest way to read a large number of small
Possible Duplicate: In Python, fastest way to build a list of files in a
Possible Duplicate: What is the Fastest Method for High Performance Sequential File I/O in
Possible Duplicates: Fastest way to list all primes below N in python Checking if
Possible Duplicate: How to do alpha blend fast? What is the fastest way to
Possible Duplicate: Compute fast log base 2 ceiling What is the fastest possible way
Possible Duplicate: Fastest way to get range complement I have a sorted array of
Possible Duplicate: Fastest way to determine if an integer's square root is an integer
Possible Duplicate: Fastest way to retrieve a <title> in PHP Suppose there is a

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.