File type

Represents a file in a file system (S_IFREG)

Direct parent types

Path
Represents file system path such as a file, a block device, etc
Direct subtypes: 7

Direct children types

MaybeFile
Like File but the programmer is aware that it might not exist. Therefore, no exceptions will be thrown for most operations on the file if it is not present.
TmpFile
Represents a temporary file. Automatically removed when the script exits.

Methods

access(f:File, mode:Int=0) Source: stdlib.ngs:7883
Check whether current process has the specified access to the file. Wrapper around ACCESS(2). Throws CError.

Parameters

modeOne of the file ACCESS::* modes, defaults to ACCESS::F_OK (test file existence only).

Returns

Bool

Example

access(File("mydata"), ACCESS::R_OK).not() throws Error("No access to data file").set("file", "mydata")
close(f:File) Source: stdlib.ngs:5837
Close a file and sets the "fd" field to null. Uses CLOSE(2). Throws InvalidArgument if file is not open. Throws FileIOFail if an underlying error occurs.

Returns

f
echo(f:File, s:Str) Source: stdlib.ngs:7933
Write a string followed by newline character to a File. Throws WriteFail on failure.

Returns

null

Example

echo(File("1.txt"), "blah")
echo(f:File, l:Lines) Source: stdlib.ngs:7942
Write a lines followed by newline character to a File. Throws WriteFail on failure.

Returns

null

Example

echo(File("1.txt"), Lines(["aaa", "bbb"]))
init(pr:ProcessRedir, fd:Any, marker:Any, datum:File) Source: stdlib.ngs:5543
Undocumented
init(f:File, path:Str) Source: stdlib.ngs:5749
Create File object from the given path. The file is not open.

Example

f = File('/tmp/myfile')
init(f:File, fd:Int) Source: stdlib.ngs:5755
Create File object from the given file descriptor.
init(f:File, path:Path) Source: stdlib.ngs:5759
Undocumented
lines(f:File, cb:Fun) Source: stdlib.ngs:5798
Iterate over lines of the file

Parameters

fClosed File
cbfunction to call with successive lines from the file
lines(f:File) Source: stdlib.ngs:5814
Get all lines of the file

Returns

Arr of Str
lines(f:File, lines_:Arr) Source: stdlib.ngs:5819
Write given lines to the file. Overwrites the file.

Returns

Arr of Str
open(f:File, flags:Str) Source: stdlib.ngs:5767
Open a file and set the "fd" field to the file descriptor. Uses OPEN(2). Throws InvalidArgument if file is already open. Throws FileIOFail if an underlying error occurs.

Parameters

flagsCurrently "r", "w" or "a" for read/write/append

Returns

f
read(f:File) Source: stdlib.ngs:7903
Read whole file
replace(f:File, src:Any, dst:Any) Source: stdlib.ngs:5493
Replace in a file. Wrapper around replace(Str, Any, Any).

Returns

f
store(file:File, data:Any, encode_hints:Hash={}) Source: stdlib.ngs:5885
encode() and write() the data to the file
write(s:Str, f:File) Source: stdlib.ngs:7919
Write whole file if it's closed, write to file descriptor if it's open
write(f:File, s:Str) Source: stdlib.ngs:7924
Write whole file if it's closed, write to file descriptor if it's open