IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

FileDescriptor

struct FileDescriptor

File descriptor of a file.

Fields

  • value (Int): The underlying value of the file descriptor.

Implemented traits

AnyType, Copyable, ImplicitlyCopyable, ImplicitlyDestructible, Movable, RegisterPassable, TrivialRegisterPassable, Writer

Methods

__init__

__init__(value: Int = 1) -> Self

Constructs the file descriptor from an integer.

Args:

  • value (Int): The file identifier (Default 1 = stdout).

__init__(f: FileHandle) -> Self

Constructs the file descriptor from a file handle.

Args:

write_bytes

write_bytes(mut self, bytes: Span[UInt8])

Write a span of bytes to the file.

Args:

  • bytes (Span[UInt8]): The byte span to write to this file.

write_string

write_string(mut self, string: StringSlice)

Write a StringSlice to this FileDescriptor.

This method is required by the Writer trait.

Args:

  • string (StringSlice): The StringSlice to write to this FileDescriptor.

read_bytes

read_bytes(mut self, buffer: Span[UInt8]) -> UInt

Read a number of bytes from the file into a buffer.

Notes: Reference.

Args:

  • buffer (Span[UInt8]): A Span[Byte] to read bytes into. Read up to len(buffer) number of bytes.

Returns:

UInt: Actual number of bytes read.

Raises:

If the operation fails.

isatty

isatty(self) -> Bool

Checks whether a file descriptor refers to a terminal.

Returns True if the file descriptor is open and connected to a tty(-like) device, otherwise False. On GPUs, the function always returns False.

Examples:

from sys import stdout

# Check if stdout is a terminal
if stdout.isatty():
print("Running in a terminal")
else:
print("Output is redirected")

Returns:

Bool: True if the file descriptor is connected to a terminal, False otherwise.