Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Nsutton

macrumors member
Original poster
Dec 29, 2009
92
0
6 Feet Under
ok so, I want to have the bottom code to search that txt file find the line that contains the search criteria and then delete the line that contains the search criteria.



Ideas?


Code:
if action2=='2':
		fob=open('/Users/NSutton/Simple Adress/bookdata.txt','r')
		searchdelete=raw_input("Enter the name of the contact you'd like to delete: ")
		for line in fob:
			if searchdelete in line:
    				print line
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
Instead of printing any line containing the search criteria, copy (append) any line that does not contain the search criteria to a destination file, or to memory. Essentially, you want to copy your source file line by line to a destination file, but skip any line where you find a match. If you want it to replace your source file, make the destination file a temp file, and when done, delete the source file and rename the temp file to the source file's name.
 

Nsutton

macrumors member
Original poster
Dec 29, 2009
92
0
6 Feet Under
Instead of printing any line containing the search criteria, copy (append) any line that does not contain the search criteria to a destination file, or to memory. Essentially, you want to copy your source file line by line to a destination file, but skip any line where you find a match. If you want it to replace your source file, make the destination file a temp file, and when done, delete the source file and rename the temp file to the source file's name.

I understand in concept but am lost in actually putting that to code....
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.