0Pricing
Python Academy · Lesson

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

  1. Opening and Reading Files
  2. Writing and Appending to Files
  3. Using the with Statement for Files
  4. Working with File Paths using pathlib
← Back to Python Academy