CHAPTER 47   •   PYTHON

Python Write Files

Learn how to write data into files.

01 What is this?

Learn how to write data into files.

02 Syntax

Python Syntax
open(filename, 'w')

03 Example

Python Example
with open('example.txt', 'w') as file:
    file.write('AIFA Infotech')

04 Important Points

Write mode can replace existing file contents.

05 Common Mistakes

Use append mode when you do not want to overwrite existing data.

06 Real World Use

Writing files is useful for reports and generated data.

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 →