The contents of the text file test.txt AAA BBB CCC DDD If
python
s = """\
AAA
BBB
DDD
DDD
"""
with open ('test.txt', 'r+') as f:
print(f.read())
f.seek(0)
f.write(s)
Execution result
AAA
BBB
CCC
DDD
The contents of the text file test.txt are AAA BBB DDD DDD become.
r + is read + write. So If you don't have test.txt Unable to read An error will occur.
Recommended Posts