Chapter 8 of 14

Files and data

Creole can read and write files with direct instructions. Test file work with temporary data before using important files.

Write a text file

Write "Hello from Creole" to file "notes.txt".

Read a text file

Read file "notes.txt" into content.
Show content.

The file content is saved in content for later instructions.

Work with folders

Make folder "reports".
List files in "reports" recursively as files.

For each file in files.
  Show file.
End.

Use clear project-relative paths. Project-managed paths should remain inside the project folder.

Copy files

Copy file "notes.txt" to "reports/notes.txt".

Check the source and destination before copying, moving, replacing, or deleting important data.

Save native data

Make record report.
  Title is "Daily report".
  Finished is true.
End.

Save report to data file "report.json".
Load data file "report.json" into saved_report.
Show saved_report in JSON.

Creole records remain native while your program works with them. The data file provides a useful storage format.

Read CSV data

Read CSV "people.csv" as people.

For each person in people.
  Show person's name.
End.

Sort and save CSV data:

Sort people by "name".
Save people as CSV "sorted.csv".

Safe file habits

  • Keep a backup before changing important files.
  • Test with a small temporary folder.
  • Use clear source and destination names.
  • Keep project data inside the project folder.
  • Check whether a file exists when its presence is uncertain.
  • Never delete a path received from untrusted input without validation.

Practice

Make folder "learning".
Write "Creole file practice" to file "learning/note.txt".
Read file "learning/note.txt" into note.
Show note.

List files in "learning" recursively as files.
For each file in files.
  Show file.
End.

Learning checklist

  • I can write and read text.
  • I can create a folder.
  • I can list files.
  • I can copy a file safely.
  • I can save and load native data.
  • I can read and save CSV data.

Next step

Continue to API connections to send and receive data over the web.