[Tutor] file open (take 2)

Dave S pythontut at pusspaws.net
Thu Sep 28 16:56:23 CEST 2006


On Wednesday 27 September 2006 21:59, Dave S wrote:
> Hi,
>
> I am trying to read in an ascii text file, do some alterations and write it
> back.
>
> file = open(self.config.get('pdf','cert') + '/cert.pdf' , 'r+')
> lines = file.readlines()
>
> ... process lines ...
>
> file.writelines(lines)
> file.close()
>
> works but ends up appending a second modified copy to the original ... as
> per the python ref.
>
> Am I right in thinking that the only way is to open with a 'r', close them
> open with a 'w' ?
>
> Cheers
>
> Dave
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

Thanks for all your input - that's great - I also learnt about 
file.rewind() and file.seek(0) :)

Thanks once again

Dave


More information about the Tutor mailing list