Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Writing CSV files


Before we start working with CSV files, let's run through a quick recap of how we can write to text files in general.

with open('sample.txt', mode='w') as file:
  file.write('sample line')

Pretty easy, right? The open() function with mode set to 'w' opens a file for writing. If the file doesn't exist, it will be automatically created. Next, we write a sample line into the file, and that's it! The with syntax closes the file for us.


Create a file named recap.txt, and write the following sentence in it:

I love Python!

Stuck? Here's a hint!

Take a look at the code in the explanation. Change the file name and the string to write to the file.