open("file_path")

The open() function opens a file for reading and writing operations, making it the currently active file for subsequent file operations. It also returns the file's contents.

Syntax

open("file_path")

Parameters

  • file_path - The path to the file to open. This can be:

    • A relative path from the current directory

    • An absolute path

    • A UUID reference to a file

Return Value

The contents of the opened file as a string.

Description

The open() function serves two purposes:

  1. It makes the specified file the "currently open file" for subsequent file operations like fileGet() and fileSet()

  2. It returns the entire contents of the file as a string

This function requires appropriate file permissions to be granted to the application.

Examples

Basic File Opening

// Open a file and get its contents
content = open("data.txt")
log "File contents: " ++ content

Opening and Processing a File

// Open a configuration file
config = open("config.json")

// Parse the JSON content
configObj = config.JsonParse()

// Access configuration values
log "Username: " ++ configObj.username
log "Theme: " ++ configObj.theme

Using File UUID

// Open a file using its UUID
fileData = open("550e8400-e29b-41d4-a716-446655440000")
log fileData

Error Handling

// Attempt to open a file with error handling
try (
  content = open("data.txt")
  log "File opened successfully"
) catch (
  log "Error opening file: " ++ error
)

Notes

  • The file remains open until another file is opened or the application closes

  • Only one file can be open at a time

  • If the file doesn't exist, an error will be thrown

  • The function requires appropriate file permissions

  • For large files, be aware of memory usage when loading the entire file content

  • The returned content is always a string; use parsing functions like JsonParse() for structured data

Last updated

Was this helpful?