thanks for the advice Alan.. I am wondering about the following: new_word = response3[2] old_word = response[word_number]
#this works but adds carriage returns********* for line in fileinput.FileInput("journey_test.txt",inplace=1): line = line.replace(old_word, new_word) print line It seems that if I put it in at the difficulty location it works, but it also adds carriage returns to the entire text file. Basically it ends up double spacing a single-spaced document. I'm also not sure if this is causing the trouble you suggested against (writing and reading at the same time). cheers On Sat, Jul 11, 2009 at 3:38 PM, ALAN GAULD <alan.ga...@btinternet.com>wrote: > > I am having trouble with probably the most simple part: > > I cannot seem to go back into the 'txt' file and replace the word I just > > searched with the new word! > > Its not a good idea to try to read and write to the same file at the same > time. The normal approach is to weither ead the file into memory and > process it then write it back to the file or to open a second file and > write to that then copy the second file over the original. > > > One with re.sub, which I can't seem to get to work > > re.sub works on a text string it doesn't affect the file. > > read the content into a string, close the input file. > Use re.sub to make the changes (or even just the replace > method of strings) then write the changed string back out > to the file. > > HTH, > > Alan G > -- Pete Froslie 617.314.0957 http://www.froslie.net
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor