read()
Read a file by path
Passing a path lets the runtime open and close the file internally.
use fs
path = fs.temporary_file()
try:
write(path, 'Atom')
print(read(path))
finally:
fs.remove(path)
Atom
read() returns the file text. The finally block removes the temporary file.
Read one message from a channel
When given a channel, read() receives the next message.
use task
queue = task.channel(1)
try:
task.send(queue, 7)
print(read(queue))
finally:
task.close(queue)
7
The returned value is the message, not a record containing it.
Reads a path, handle, socket or channel.
Signature
read(source[, option])
Parameters
Path plus optional 'r', 'rb' or nonnegative signed byte limit. Open file plus optional nonnegative signed byte limit. Socket plus optional positive signed byte limit, using the socket timeout. Channel plus optional timeout. Requires the libuv build.
Returns
File text or packed bytes, TCP bytes, UDP packet dictionary, or a channel message. Closed, drained channel reads return null.
Behavior
Blocks as applicable and advances a file, consumes socket data or dequeues one message. A path read opens and closes its file internally. File reads without a byte limit read to EOF. At EOF an open file returns the empty value for its text or binary mode.
Errors
Host permission, invalid handle or limit, decode failure, I/O error or timeout.
Example
use fs
p = fs.temporary_file()
try:
write(p, 'Atom')
assert(read(p) == 'Atom')
finally:
fs.remove(p)