Skip to main content

Mojo module

file

Provides APIs to read and write files.

These are Mojo built-ins, so you don't need to import them.

For example, here's how to read a file:

var  f = open("my_file.txt", "r")
print(f.read())
f.close()

Or use a with statement to close the file automatically:

with open("my_file.txt", "r") as f:
  print(f.read())

comptime values

O_APPEND

comptime O_APPEND = platform_map[Optional[String]("O_APPEND"), linux=Optional[Int](1024), macos=Optional[Int](8)]()

Append mode: writes always go to end of file.

O_CLOEXEC

comptime O_CLOEXEC = platform_map[Optional[String]("O_CLOEXEC"), linux=Optional[Int](524288), macos=Optional[Int](16777216)]()

Close file descriptor on exec.

O_CREAT

comptime O_CREAT = platform_map[Optional[String]("O_CREAT"), linux=Optional[Int](64), macos=Optional[Int](512)]()

Create file if it doesn't exist.

O_RDONLY

comptime O_RDONLY = 0

Open file for reading only.

O_RDWR

comptime O_RDWR = 2

Open file for reading and writing.

O_TRUNC

comptime O_TRUNC = platform_map[Optional[String]("O_TRUNC"), linux=Optional[Int](512), macos=Optional[Int](1024)]()

Truncate file to zero length.

O_WRONLY

comptime O_WRONLY = 1

Open file for writing only.

Structs

Functions

  • open: Opens the file specified by path using the mode provided, returning a FileHandle.

Was this page helpful?