[Tutor] Deleting Lines of text
pythonhack@yahoo.com
pythonhack@yahoo.com
Tue, 16 Apr 2002 11:47:30 -0700
as far as i know, it's not possible to edit a file in place. you'll
have to read the contents of the file into memory, make whatever
changes you want, then write it back to a different file. like this (assuming
test.txt is the one you want to edit:
newfile = open('test2.txt', 'w')
file = open('test.txt', 'r')
lines = file.readlines()
line = lines.split()
for line in lines:
if 'BobH' not in line:
newfile.write(line):
file.close()
newfile.close()
**i didn't test this, so it might not be exact, but that's the idea...
brett
BX> How do I delete a line of text in a file? I want to find the word "BobH" and delete the line it is on and then close the file.
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com