python how to remove spacific line in file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



python how to remove spacific line in file



I'm trying to delete from a file.



This is what I am trying to do.



1.importing Easygui



2.Put three modes plus quitting



Everything is working except deleting part.
this is what I have


import easygui

filea = open('GroceryList.txt', 'a')
fr = open('GroceryList.txt', 'r')
filer = open('GroceryList.txt', 'r')

runinng = True
while runinng:
a = easygui.choicebox('Do you want to add, delete, or see your list?',
choices = ['add', 'delete', 'see', 'quit'])
if a == 'add':
ad = easygui.enterbox('What do you want to add')
filea.write(ad)
filea.write('n')
filea.close()
filea = open('GroceryList.txt', 'a')


elif a == 'delete':
rn = easygui.enterbox('What are you going to delete?')
rl = fr.readlines()
for lines in fr:
if rn in lines:
line.split()
fr.close()
fr = open('GroceryList.txt', 'r')


elif a == 'see':
s = filer.readlines()
easygui.msgbox(s)
filer.close()
filer = open('GroceryList.txt', 'r')

elif a == 'quit':
runinng = False
filea.close()
fr.close()
filer.close()



The part that is not working is:


elif a == 'delete':
rn = easygui.enterbox('What are you going to delete?')
rl = fr.readlines()
if rn in fr:
line.remove()
fr.close()
fr = open('GroceryList.txt', 'r')





You need to open a new file with write access (w). Loop reading each line from the original and write the line if it is not the one to delete. Close both files then rename the new file to be the same as the old. A bit of a pain, but that's what happens if you use a text file instead of a database.
– cdarke
Aug 10 at 12:35



w





A line is best deleted from a file this way: 1 read all lines from file into a list, 2 delete one line from the list, 3 open the file again, this time for writing, and write the remaining lines into it
– zvone
Aug 10 at 12:36





Thanks for the advice, but... I don't get it. Can you show me a script?
– Death
Aug 10 at 12:43





2 Answers
2



As others mentioned in the comments just providing a small script to clear out the doubts you may still have


fr = open('GroceryList.txt', 'r')
rl = fr.readlines()
new_list_to_keep_rows=
for row in rl:
#add rows to keep
new_list_to_keep_rows.append(row)

new_file = open('GroceryList_new.txt', 'w')# open file in write mode
for item in new_list_to_keep_rows:
new_file.write("%sn" % item)



rename the file when you are sure you have everything.





Thanks but I got an error: Traceback (most recent call last): File "C:/Python27/Hello world/editGroceryList.py", line 26, in <module> new_list_to_keep_rows.add(row) AttributeError: 'list' object has no attribute 'add'
– Death
Aug 10 at 15:57






News: There where no errors but it didn't work. Sorry. :( but good job
– Death
Aug 10 at 16:04






Does anyone have a simple script?
– Death
Aug 10 at 16:13






There cannot be a simpler script than this. You are dealing with two different file modes here. Good luck
– mad_
Aug 10 at 16:16





Ah well it was worth it sigh
– Death
Aug 10 at 16:18



Try this.


p = "C:\Path\To\Your\File\myfile.txt"
f = open(p, "r+") # Open file
f.seek(0) # Goto top line in file
d = f.readlines() # Read all lines from top to bottom and save them in variable
for l in d: # Check if any line matches line you want to remove
if l != "Line to remove":
f.write(l + "n") # Write all lines except the one you dont want to
f.close()
print("Data saved to" + " " + f.name) # Optional



If you want to use this you have to change your code otherwise you will get IOErrors. Put your code in class and run it with proper handling.





Dude, it's not working and i get this (Error: (2, 'No such file or directory') Error: 2, 'No such file or directory') Error: 2, 'No such file or directory' Error: 2, 'No such file or directory' Error: 2, 'No such file or directory' Error: 2 'No such file or directory') Also guys I'm using python 2.7.5
– Death
Aug 10 at 18:41






Instead of "myfile.txt" put full path to your file. And since you have python version 2.7.5 put IO Exception instead of Exception.
– Tit Poplatnik
Aug 11 at 10:19






I got two errors: Traceback (most recent call last): File "C:/Python27/Hello world/editGroceryList.py", line 35, in <module> for fchar in forbid: NameError: name 'forbid' is not defined
– Death
Aug 11 at 12:14


Traceback (most recent call last): File "C:/Python27/Hello world/editGroceryList.py", line 35, in <module> for fchar in forbid: NameError: name 'forbid' is not defined





Also I got a syntax error with the Exception part.
– Death
Aug 11 at 12:16





Ill edit the code for you and adjust it for your needs.
– Tit Poplatnik
Aug 11 at 18:36






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard