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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:21:08+00:00 2026-06-06T17:21:08+00:00

I have a file where each line starts with a number. The user can

  • 0

I have a file where each line starts with a number. The user can delete a row by typing in the number of the row the user would like to delete.

The issue I’m having is setting the mode for opening it. When I use a+, the original content is still there. However, tacked onto the end of the file are the lines that I want to keep. On the other hand, when I use w+, the entire file is deleted. I’m sure there is a better way than opening it with w+ mode, deleting everything, and then re-opening it and appending the lines.

 def DeleteToDo(self):
    print "Which Item Do You Want To Delete?"
    DeleteItem = raw_input(">") #select a line number to delete
    print "Are You Sure You Want To Delete Number" + DeleteItem + "(y/n)"
    VerifyDelete = str.lower(raw_input(">"))
    if VerifyDelete == "y":
        FILE = open(ToDo.filename,"a+") #open the file (tried w+ as well, entire file is deleted)
        FileLines = FILE.readlines() #read and display the lines
        for line in FileLines:
            FILE.truncate()
            if line[0:1] != DeleteItem: #if the number (first character) of the current line doesn't equal the number to be deleted, re-write that line
                FILE.write(line)
    else:
        print "Nothing Deleted"

This is what a typical file may look like

1. info here
2. more stuff here
3. even more stuff here
  • 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-06T17:21:09+00:00Added an answer on June 6, 2026 at 5:21 pm
    def DeleteToDo():
        print ("Which Item Do You Want To Delete?")
        DeleteItem = raw_input(">") #select a line number to delete
        print ("Are You Sure You Want To Delete Number" + DeleteItem + "(y/n)")
        DeleteItem=int(DeleteItem) 
        VerifyDelete = str.lower(raw_input(">"))
        if VerifyDelete == "y":
            FILE = open('data.txt',"r") #open the file (tried w+ as well, entire file is deleted)
            lines=[x.strip() for x in FILE if int(x[:x.index('.')])!=DeleteItem] #read all the lines first except the line which matches the line number to be deleted
            FILE.close()
            FILE = open('data.txt',"w")#open the file again
            for x in lines:FILE.write(x+'\n')    #write the data to the file
    
        else:
            print ("Nothing Deleted")
    DeleteToDo()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a test file that has many lines, each line looks something like:
I have a text file where each line may end with some fixed TAG
I have a large file where each line contains a substring such as ABC123.
I have a file in which each line contains two numbers. The problem is
I have a set of lines in a file where each line might represent
I have a file with 200mb, the file with following format each line is
I have a file (10-20MB) containing data, where each line is a single piece
If I have a text file with a separate command on each line how
I have a list URLs in a file called urls.txt . Each line contains
I have a plain text file with a number of lines (new line character

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.