file
Module
Implements the file based methods.
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())
FileHandle
File handle to an opened file.
Fields:
- βhandle (
DTypePointer[invalid, 0]
): The underlying pointer to the file handle.
Functions:
__init__
__init__(inout self: Self)
Default constructor.
__init__(inout self: Self, path: StringLiteral, mode: StringLiteral)
Construct the FileHandle using the file path and mode.
Args:
- βpath (
StringLiteral
): The file path. - βmode (
StringLiteral
): The mode to open the file in (the mode can be βrβ or βwβ).
__init__(inout self: Self, path: String, mode: String)
Construct the FileHandle using the file path and mode.
Args:
- βpath (
String
): The file path. - βmode (
String
): The mode to open the file in (the mode can be βrβ or βwβ).
__init__(inout self: Self, path: StringRef, mode: StringRef)
Construct the FileHandle using the file path and string.
Args:
- βpath (
StringRef
): The file path. - βmode (
StringRef
): The mode to open the file in (the mode can be βrβ or βwβ).
__moveinit__
__moveinit__(inout self: Self, owned existing: Self)
Moves constructor for the file handle.
Args:
- βexisting (
Self
): The existing file handle.
__takeinit__
__takeinit__(inout self: Self, inout existing: Self)
Moves constructor for the file handle.
Args:
- βexisting (
Self
): The existing file handle.
__del__
__del__(owned self: Self)
Closes the file handle.
close
close(inout self: Self)
Closes the file handle.
read
read(self: Self, size: SIMD[si64, 1]) -> String
Reads the data from the file.
Args:
- βsize (
SIMD[si64, 1]
): Requested number of bytes to read.
Returns:
The contents of the file.
read_bytes
read_bytes(self: Self, size: SIMD[si64, 1]) -> Tensor[si8]
Read from file buffer until we have size
characters or we hit EOF. If size
is negative or omitted, read until EOF.
Args:
- βsize (
SIMD[si64, 1]
): Requested number of bytes to read.
Returns:
The contents of the file.
seek
seek(self: Self, offset: SIMD[ui64, 1]) -> SIMD[ui64, 1]
Seeks to the given offset in the file.
Raises: An error if this file handle is invalid, or if file seek returned a failure.
Args:
- βoffset (
SIMD[ui64, 1]
): The byte offset to seek to from the start of the file.
Returns:
The resulting byte offset from the start of the file.
write
write(self: Self, data: StringLiteral)
Write the data to the file.
Args:
- βdata (
StringLiteral
): The data to write to the file.
write(self: Self, data: String)
Write the data to the file.
Args:
- βdata (
String
): The data to write to the file.
write(self: Self, data: StringRef)
Write the data to the file.
Args:
- βdata (
StringRef
): The data to write to the file.
__enter__
__enter__(owned self: Self) -> Self
The function to call when entering the context.
open
open(path: StringLiteral, mode: StringLiteral) -> FileHandle
Opens the file specified by path using the mode provided, returning a FileHandle.
Args:
- βpath (
StringLiteral
): The path to the file to open. - βmode (
StringLiteral
): The mode to open the file in (the mode can be βrβ or βwβ).
Returns:
A file handle.
open(path: StringRef, mode: StringRef) -> FileHandle
Opens the file specified by path using the mode provided, returning a FileHandle.
Args:
- βpath (
StringRef
): The path to the file to open. - βmode (
StringRef
): The mode to open the file in (the mode can be βrβ or βwβ).
Returns:
A file handle.
open(path: String, mode: String) -> FileHandle
Opens the file specified by path using the mode provided, returning a FileHandle.
Args:
- βpath (
String
): The path to the file to open. - βmode (
String
): The mode to open the file in.
Returns:
A file handle.
open(path: Path, mode: String) -> FileHandle
Opens the file specified by path using the mode provided, returning a FileHandle.
Args:
- βpath (
Path
): The path to the file to open. - βmode (
String
): The mode to open the file in (the mode can be βrβ or βwβ).
Returns:
A file handle.