I am trying to learn how to open txt.files on Python 3.7. I have a file (Days.txt) with the following content:
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
On Jupyter, I used the following code:
f = open('Days.txt','r').read()
f
which gave me the following result:
'Monday\nTuesday\nWednesday\nThursday\nFriday\nSaturday\nSunday'
so far, so good.
Then, I tried to add a sentence to the Days.txt file by:
f.write('this is a test\n')
and that's where I get the below error message:
AttributeError Traceback (most recent call last) in ----> 1 f.write('this is a test\n')
AttributeError: 'str' object has no attribute 'write'
Why isn't it working?
The code below results in another error message:
file = open('Days.txt','r')
s = file.read()
file.write('this is a test\n')
file.close()
---------------------------------------------------------------------------
UnsupportedOperation Traceback (most recent call last)
<ipython-input-138-0cc4cd12a8b3> in <module>
1 file = open('Days.txt','r')
2 s = file.read()
----> 3 file.write('this is a test\n')
4 file.close()
UnsupportedOperation: not writable