|
|
#1 |
|
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. |
|
|
|
0
|
|
|
#2 |
|
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! |
|
|
|
0
|
|
|
#3 | |
|
Quote:
__________________
An Apple a day keeps viruses away!
|
||
|
|
0
|
|
|
#4 |
|
It seems like changing your if to say not in line, that would do it.
-Lee |
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 01:37 AM.




An Apple a day keeps viruses away! 


Linear Mode

