Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 52 / 68

The basics of reading and writing files

This lesson covers reading and writing files in Julia, with the aim of saving and loading text from a program.

Combining open() with a do block gives you a safe form that opens a file, does the work, and closes it automatically. Saving configuration and logs makes this a basic everyday operation.

The sample code opens a file with open("memo.txt", "w") do f ... end and saves the contents with write(f, ...). Note that the file is closed automatically on leaving the do block.

A common early stumble is the do block itself. It is really syntax for passing an anonymous function, which is confusing if you have not met callbacks before.

In professional work, that do block is valued as a safe resource-management mechanism that heads off the bug of forgetting to close a file.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)