Writing and Appending to Files
Write new data and append to existing files safely.
Introduction
Writing and appending to files lets your programs persist data between runs.
Write Mode 'w'
open('out.txt', 'w') creates the file (or truncates if it exists). f.write('Hello') writes the string.
# with open('out.txt', 'w') as f:
# f.write('Hello, World!')
print('write mode demo')All lessons in this course
- Opening and Reading Files
- Writing and Appending to Files
- Using the with Statement for Files
- Working with File Paths using pathlib