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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:16:57+00:00 2026-05-11T04:16:57+00:00

I have an application that reads lines from a file and runs its magic

  • 0

I have an application that reads lines from a file and runs its magic on each line as it is read. Once the line is read and properly processed, I would like to delete the line from the file. A backup of the removed line is already being kept. I would like to do something like

file = open('myfile.txt', 'rw+') for line in file:    processLine(line)    file.truncate(line) 

This seems like a simple problem, but I would like to do it right rather than a whole lot of complicated seek() and tell() calls.

Maybe all I really want to do is remove a particular line from a file.

After spending far to long on this problem I decided that everyone was probably right and this it just not a good way to do things. It just seemed so elegant solution. What I was looking for was something akin to a FIFO that would just let me pop lines out of a 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. 2026-05-11T04:16:57+00:00Added an answer on May 11, 2026 at 4:16 am

    Remove all lines after you’ve done with them:

    with open('myfile.txt', 'r+') as file:     for line in file:         processLine(line)     file.truncate(0) 

    Remove each line independently:

    lines = open('myfile.txt').readlines()  for line in lines[::-1]: # process lines in reverse order     processLine(line)     del lines[-1]  # remove the [last] line  open('myfile.txt', 'w').writelines(lines) 

    You can leave only those lines that cause exceptions:

    import fileinput, sys      for line in fileinput.input(['myfile.txt'], inplace=1):     try: processLine(line)     except Exception:          sys.stdout.write(line) # it prints to 'myfile.txt' 

    In general, as other people already said it is a bad idea what you are trying to do.

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

Sidebar

Ask A Question

Stats

  • Questions 243k
  • Answers 243k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer See this CodeCentral article: Using more than 3 GB memory… May 13, 2026 at 7:46 am
  • Editorial Team
    Editorial Team added an answer You can use IList with <bag> (and <idbag> I believe).… May 13, 2026 at 7:46 am
  • Editorial Team
    Editorial Team added an answer The HTML spec states that there are three kinds of… May 13, 2026 at 7:46 am

Related Questions

I'm facing this strange problem. I'm trying to read a file that is located
I'm writing an application in Python that is going to have a lot of
Given a text file, how would I go about reading an arbitrary line and
I have two computers. Both running WinXP SP2 (I don't really know ho similar

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.