Skip to main content
Log in

Mojo struct

FileHandle

struct FileHandle

File handle to an opened file.

Fields

  • handle (UnsafePointer[NoneType]): The underlying pointer to the file handle.

Implemented traits

AnyType, UnknownDestructibility, Writer

Methods

__init__

__init__(out self)

Default constructor.

__init__(out self, path: StringSlice[origin], mode: StringSlice[origin])

Construct the FileHandle using the file path and mode.

Args:

  • path (StringSlice[origin]): The file path.
  • mode (StringSlice[origin]): The mode to open the file in (the mode can be "r" or "w" or "rw").

__moveinit__

__moveinit__(out self, owned existing: Self)

Moves constructor for the file handle.

Args:

  • existing (Self): The existing file handle.

__del__

__del__(owned self)

Closes the file handle.

close

close(mut self)

Closes the file handle.

read

read(self, size: Int = -1) -> String

Reads data from a file and sets the file handle seek position. If size is left as the default of -1, it will read to the end of the file. Setting size to a number larger than what's in the file will set the String length to the total number of bytes, and read all the data.

Examples:

Read the entire file into a String:

var file = open("/tmp/example.txt", "r")
var string = file.read()
print(string)
var file = open("/tmp/example.txt", "r")
var string = file.read()
print(string)

Read the first 8 bytes, skip 2 bytes, and then read the next 8 bytes:

import os
var file = open("/tmp/example.txt", "r")
var word1 = file.read(8)
print(word1)
_ = file.seek(2, os.SEEK_CUR)
var word2 = file.read(8)
print(word2)
import os
var file = open("/tmp/example.txt", "r")
var word1 = file.read(8)
print(word1)
_ = file.seek(2, os.SEEK_CUR)
var word2 = file.read(8)
print(word2)

Read the last 8 bytes in the file, then the first 8 bytes

_ = file.seek(-8, os.SEEK_END)
var last_word = file.read(8)
print(last_word)
_ = file.seek(8, os.SEEK_SET) # os.SEEK_SET is the default start of file
var first_word = file.read(8)
print(first_word)
_ = file.seek(-8, os.SEEK_END)
var last_word = file.read(8)
print(last_word)
_ = file.seek(8, os.SEEK_SET) # os.SEEK_SET is the default start of file
var first_word = file.read(8)
print(first_word)

.

Args:

  • size (Int): Requested number of bytes to read (Default: -1 = EOF).

Returns:

The contents of the file.

Raises:

An error if this file handle is invalid, or if the file read returned a failure.

read_bytes

read_bytes(self, size: Int = -1) -> List[SIMD[uint8, 1]]

Reads data from a file and sets the file handle seek position. If size is left as default of -1, it will read to the end of the file. Setting size to a number larger than what's in the file will be handled and set the List length to the total number of bytes in the file.

Examples:

Reading the entire file into a List[Int8]:

var file = open("/tmp/example.txt", "r")
var string = file.read_bytes()
var file = open("/tmp/example.txt", "r")
var string = file.read_bytes()

Reading the first 8 bytes, skipping 2 bytes, and then reading the next 8 bytes:

import os
var file = open("/tmp/example.txt", "r")
var list1 = file.read(8)
_ = file.seek(2, os.SEEK_CUR)
var list2 = file.read(8)
import os
var file = open("/tmp/example.txt", "r")
var list1 = file.read(8)
_ = file.seek(2, os.SEEK_CUR)
var list2 = file.read(8)

Reading the last 8 bytes in the file, then the first 8 bytes:

import os
var file = open("/tmp/example.txt", "r")
_ = file.seek(-8, os.SEEK_END)
var last_data = file.read(8)
_ = file.seek(8, os.SEEK_SET) # os.SEEK_SET is the default start of file
var first_data = file.read(8)
import os
var file = open("/tmp/example.txt", "r")
_ = file.seek(-8, os.SEEK_END)
var last_data = file.read(8)
_ = file.seek(8, os.SEEK_SET) # os.SEEK_SET is the default start of file
var first_data = file.read(8)

.

Args:

  • size (Int): Requested number of bytes to read (Default: -1 = EOF).

Returns:

The contents of the file.

Raises:

An error if this file handle is invalid, or if the file read returned a failure.

seek

seek(self, offset: SIMD[uint64, 1], whence: SIMD[uint8, 1] = __init__[__mlir_type.!pop.int_literal](0)) -> SIMD[uint64, 1]

Seeks to the given offset in the file.

Examples:

Skip 32 bytes from the current read position:

import os
var f = open("/tmp/example.txt", "r")
_ = f.seek(32, os.SEEK_CUR)
import os
var f = open("/tmp/example.txt", "r")
_ = f.seek(32, os.SEEK_CUR)

Start from 32 bytes from the end of the file:

import os
var f = open("/tmp/example.txt", "r")
_ = f.seek(-32, os.SEEK_END)
import os
var f = open("/tmp/example.txt", "r")
_ = f.seek(-32, os.SEEK_END)

.

Args:

  • offset (SIMD[uint64, 1]): The byte offset to seek to.
  • whence (SIMD[uint8, 1]): The reference point for the offset: os.SEEK_SET = 0: start of file (Default). os.SEEK_CUR = 1: current position. os.SEEK_END = 2: end of file.

Returns:

The resulting byte offset from the start of the file.

Raises:

An error if this file handle is invalid, or if file seek returned a failure.

write_bytes

write_bytes(mut self, bytes: Span[SIMD[uint8, 1], origin])

Write a span of bytes to the file.

Args:

  • bytes (Span[SIMD[uint8, 1], origin]): The byte span to write to this file.

write

write[*Ts: Writable](mut self, *args: *Ts)

Write a sequence of Writable arguments to the provided Writer.

Parameters:

  • *Ts (Writable): Types of the provided argument sequence.

Args:

  • *args (*Ts): Sequence of arguments to write to this Writer.

__enter__

__enter__(owned self) -> Self

The function to call when entering the context.

Returns:

The file handle.