CHAPTER 45   •   PYTHON

Python File Handling

Learn how to create, read and write files.

01 What is this?

Learn how to create, read and write files.

02 Syntax

Python Syntax
open(filename, mode)

03 Example

Python Example
file = open('example.txt', 'w')
file.write('Hello Python')
file.close()

04 Important Points

Always close files or use a with statement.

05 Common Mistakes

Use the correct file mode.

06 Real World Use

File handling is used for reports, logs and data storage.

07 Practice This Topic

Now try the example yourself in the AIFA Python Editor.

  • Read the example carefully.
  • Change the values.
  • Run the code.
  • Observe the output.
  • Try creating your own example.
Try It Yourself →