open
fn open(string path, string mode) -> Result<Stream, IoError> Import
Source: use io::{open}; compiler/src/builtin_docs/io.hy Opens the file at path with mode and returns a stream on it.
Modes:
"r": read only; the file must exist."w": write only; creates the file or truncates it."a": append; creates the file if needed, writes go to the end."rw": read and write; creates the file if needed, does not truncate.
Any other mode returns Err(IoError::InvalidInput). OS errors map to
NotFound, PermissionDenied, NotADirectory and so on.
A program that reaches open compiles only with the matching grant: a
literal "r" needs --allow-read, "w" and "a" need --allow-write,
"rw" and a mode computed at run time need both.
Parameters#
| Name | Type |
|---|---|
path | string |
mode | string |
Returns#
Result<Stream, IoError>
Examples#
use io::{close, open};
use io::fs::{remove_file};
use io::sync::{read_to_end, write_all};
use string::{to_bytes};
use tests::assert;
let path = "/tmp/coil_doc_open.txt";
let w = match open(path, "w") {
Result::Ok(s) => s,
Result::Err(_) => panic "open failed",
};
let _ = write_all(w, to_bytes("ab"));
let _ = close(w);
let a = match open(path, "a") {
Result::Ok(s) => s,
Result::Err(_) => panic "open failed",
};
let _ = write_all(a, to_bytes("cd"));
let _ = close(a);
let r = match open(path, "r") {
Result::Ok(s) => s,
Result::Err(_) => panic "open failed",
};
let size = match read_to_end(r) {
Result::Ok(bytes) => bytes.len(),
Result::Err(_) => 0,
};
let _ = close(r);
let _ = remove_file(path);
assert!(size == 4);
See also#
stdin: Returns a new stream on the process’s standard input.stdout: Returns a new stream on the process’s standard output.stderr: Returns a new stream on the process’s standard error.capture_begin: Starts keeping this thread’s stdout and stderr writes in memory instead of writing them, until the matchingcapture_end().capture_end: Ends the innermostcapture_begin()and returns what it kept, as(stdout, stderr).close: Closess.read: Reads bytes fromsinto the front ofbufwithout waiting.write: Writes bytes frombuftoswithout waiting and returns how many were written.