Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 24, 2010, 01:14 PM   #1
Nsutton
macrumors member
 
Join Date: Dec 2009
Location: 6 Feet Under
Python removing lines from a text file

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
__________________
An Apple a day keeps viruses away!

Last edited by Nsutton; Apr 24, 2010 at 02:17 PM.
Nsutton is offline   0 Reply With Quote
Old Apr 24, 2010, 01:41 PM   #2
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: 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.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Apr 24, 2010, 02:06 PM   #3
Nsutton
Thread Starter
macrumors member
 
Join Date: Dec 2009
Location: 6 Feet Under
Quote:
Originally Posted by HiRez View Post
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....
__________________
An Apple a day keeps viruses away!
Nsutton is offline   0 Reply With Quote
Old Apr 24, 2010, 02:10 PM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
It seems like changing your if to say not in line, that would do it.

-Lee
lee1210 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Terminal, add text to the end of all text files in a directory. Jesse Smith Mac Basics and Help 2 Feb 22, 2011 02:13 PM
Display information from a text file in a HTML document Niwa Rose Web Design and Development 13 Dec 8, 2009 01:40 PM
Batch removing silence from WAV audio files wrldwzrd89 Digital Audio 7 Mar 25, 2009 04:26 PM
Reading from a text file into an array jeremyn9 Mac Programming 2 Jan 6, 2009 10:23 AM
Removing DRM From Purchased .aac files AirborneAngel iPod 8 Oct 2, 2007 02:22 AM


All times are GMT -5. The time now is 12:09 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC