Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Python Fundamentals

Lesson 19 of 55 · Python

File I/O

Duration: 20 minutes

File I/O\n\nReading from and writing to files is straightforward with Python’s built‑in open() function, especially when used inside a with context manager.\n\npython\n# Write to a file\nwith open('example.txt', 'w') as f:\n f.write('First line\nSecond line')\n\n# Read the whole file\nwith open('example.txt', 'r') as f:\n content = f.read()\n print(content)\n\n\nThe with block guarantees that the file is properly closed, even if an error occurs inside the block.\n

Previous: String ManipulationNext: Exception Handling