Skip to main content

File

The File module provides comprehensive file system operations for Soul, enabling file I/O, directory management, and file manipulation capabilities. It supports both absolute and relative paths, with automatic path resolution based on the current working directory or environment context.

File I/O Operations

read - Read file contents

Read the entire contents of a file as a string:

write - Write content to file

Write content to a file, creating it if it doesn’t exist:

append - Append content to file

Append content to an existing file, creating it if it doesn’t exist:

File Operations

copy - Copy file

Copy a file from source to destination:

move - Move/rename file

Move or rename a file:

rename - Rename file (alias for move)

Rename a file (alias for the move operation):

delete - Delete file

Delete a file:

Directory Operations

mkdir - Create directory

Create a directory (and parent directories if needed):

rmdir - Remove directory

Remove a directory and all its contents:

list - List directory contents

List files and directories in a directory:

File Information

exists - Check if file/directory exists

Check if a file or directory exists:

isFile - Check if path is a file

Check if a path points to a regular file:

isDir - Check if path is a directory

Check if a path points to a directory:

size - Get file size

Get the size of a file in bytes:

modified - Get last modified time

Get the last modified time of a file as Unix timestamp:

Path Handling

Absolute vs Relative Paths

The File module handles both absolute and relative paths:

Practical Examples

File Processing Pipeline

Backup System

Configuration Manager

File Synchronization

File Cleanup Utility

File Statistics Reporter

Error Handling

Safe File Operations

Best Practices

  1. Always check existence: Use exists() before performing operations on files
  2. Handle paths correctly: Be aware of relative vs absolute path behavior
  3. Use appropriate file types: Check isFile() vs isDir() before operations
  4. Create directories as needed: Use mkdir() to ensure directory structure exists
  5. Clean up resources: Remove temporary files after use
  6. Error handling: Check return values and handle file operation failures
The File module provides essential file system operations for Soul applications, enabling powerful file manipulation, directory management, and I/O capabilities with automatic path resolution and comprehensive error handling.