[Tutor] Simultaneous read and write on file

Anshu Kumar anshu.kumar726 at gmail.com
Mon Jan 18 11:01:41 EST 2016


Hello Everyone,

I try below code in python 2.7.10 to first create and write into a file and
then read and write that file but what i get is just a  file with new
content.


>>> with open('test.txt', 'wb+') as f:
...     f.write('this is test file.')
...     f.write('ok!!!')
...
>>> with open('test.txt', 'wb+') as f:
...     a_str = f.read() + 'read the file'
...     f.seek(0)
...     f.write(a_str)
...


I have read in documentation that wb+ mode is for writing and reading. Am i
using wrong mode, should i use rb+ ?

Thanks and regards,
Anshu


More information about the Tutor mailing list