[Tutor] thesaurus

ALAN GAULD alan.gauld at btinternet.com
Sat Jul 11 21:38:54 CEST 2009


> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090711/a5286ab0/attachment.htm>


More information about the Tutor mailing list