[Tutor] using python to write web page

Steven D'Aprano steve at pearwood.info
Thu Jun 16 02:53:02 CEST 2011


naheed arafat wrote:
> Got a question in this context. If i would like to edit an html file.
> suppose i want to edit the values of href tags or the img tags, what should
> i do?

This question is more general than just editing HTML files. The same 
question, and answer, applies to editing *any* file small enough to fit 
in memory. (If you want to edit 30 GB video files, you don't want to do 
this...)

* read the file
* make the changes you want to make
* write the file back

That's good enough for quick scripts, but for serious applications you 
want to be more careful about avoiding data loss. For example you might 
do this:

* read the file
* make the changes you want to make
* write the file to a temporary file
* atomically replace the original with the new version

or this:

* read the file
* make the changes you want to make
* save a backup copy of the original
* write the file over the original
* if that succeeds, remove the backup copy

or similar.




-- 
Steven



More information about the Tutor mailing list