File handling

File handling is how we work with files in a computer program. This includes:

  • Opening files

  • Reading from files

  • Writing to files

  • Closing files

File handling allows us to save and access data so it stays available even after the program has finished running.

Types of files

Text files and CSV files are basically the same underneath, but CSV files can be shown in a table format when opened in a spreadsheet program like Excel.

Text files (.txt)

Text files store plain text. You can open them in a simple text editor like Notepad.

Text file opened in text editor

John,Smith,A
Jane,Patel,C
Liam,Chen,B
 

CSV files (.csv)

CSV  stands for Comma-Separated Values. These files are used to store data in a table format, like a spreadsheet.

CSV opened in text editor

John,Smith,A
Jane,Patel,C
Liam,Chen,B
 
CSV opened in spreadsheet
John Smith A
Jane Patel C
Liam Chen B