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: Nightly
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).

RawFileDescriptor

struct RawFileDescriptor

File descriptor of a file.

A file descriptor is always a non-negative integer, so -1 is never a valid descriptor. RawFileDescriptor exposes -1 as its niche, which makes Optional[RawFileDescriptor] the same size as RawFileDescriptor; use Optional[RawFileDescriptor] (not a -1 value) to represent an absent descriptor.

Implemented traits​

AnyType, Copyable, Deinitable, ImplicitlyCopyable, Movable, RegisterPassable, TrivialRegisterPassable, UnsafeNicheable, UnsafeSingleNicheable, Writer

Methods​

__init__​

def __init__(value: Int32 = Int32(1)) -> Self

Constructs the file descriptor from an integer.

Constraints:

value must be non-negative. File descriptors are never negative, and -1 is the type's niche value, so a negative value aborts at runtime. Use Optional[RawFileDescriptor] to represent an absent descriptor.

Args:

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

def __init__(f: FileHandle) -> Self

Constructs the file descriptor from a file handle.

Args:

write_bytes​

def 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​

def write_string(mut self, string: StringSpan)

Write a StringSlice to this RawFileDescriptor.

This method is required by the Writer trait.

Args:

  • ​string (StringSpan): The StringSlice to write to this RawFileDescriptor.

read_bytes​

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

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:

Int: Actual number of bytes read.

Raises:

If the operation fails.

isatty​

def 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 std.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.

fchdir​

def fchdir(self)

Changes the current working directory to the one represented by this fd.

Raises:

If the operations fails. In particular if the fd is not a directory.