1
s = """\
AAA
BBB
CCC
DDD
"""
with open ('test.txt', 'w') as f:
f.write(s)
with open ('test.txt', 'r') as f:
print(f.read())
Execution result of 1
AAA
BBB
CCC
DDD
this
2
s = """\
AAA
BBB
CCC
DDD
"""
with open ('test.txt', 'w+') as f:
f.write(s)
f.seek(0)
print(f.read())
Can be written. After writing, the index will be the last, so
python
f.seek(0)
You need to go back to the beginning with.
w + is Write + read.
python
with open ('test.txt', 'w+') as f:
print(f.read())
Then First go into write mode Once empty, Do not write anything Because it will be read Nothing is output.
Recommended Posts