[Tutor] how to check whether a file exists or not??

John Fouhy john at fouhy.net
Thu Oct 12 23:13:22 CEST 2006


On 13/10/06, Asrarahmed Kadri <ajkadri at googlemail.com> wrote:
> Also if a file exists, and we open it in a write mode, it would be truncated
> to zero length. So what is the way out of this??#

You can open in append mode..

>>> f = open('foo', 'w')
>>> f.write('Hello ')
>>> f.close()
>>> f = open('foo', 'a')
>>> f.write('world!')
>>> f.close()
>>> f = open('foo', 'r')
>>> f.read()
'Hello world!'

-- 
John.


More information about the Tutor mailing list