file

package
v0.0.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 10 Imported by: 0

README

file

file provides functions to interact with the file system. The library is inspired by file helpers from Amoy.

Functions

trim_bom(rd) string

Removes the Byte Order Mark (BOM) from a byte literal string or bytes.

Parameters
name type description
rd `string byes`
Examples

String

Removes the Byte Order Mark (BOM) from a string.

load("file", "trim_bom")
s = b'\xef\xbb\xbfhello'
print(trim_bom(s))
# Output: hello
count_lines(name) int

Counts the total lines in a file located at the provided path.

Parameters
name type description
name string The path of the file whose lines are to be counted
Examples

String

Count the lines of a file.

load("file", "count_lines")
name = 'path/to/file.txt'
print(count_lines(name))
# Output: 10
head_lines(name, n) []string

Returns the first 'n' lines of a file.

Parameters
name type description
name string The path of the file
n int The number of lines from the top to be returned
Examples

String

Get the top 10 lines of a file.

load('file', 'head_lines')
print(head_lines('path/to/file.txt', 10))
# Output: ['line1', 'line2', ... 'line10']
tail_lines(name, n) []string

Returns the last 'n' lines of a file.

Parameters
name type description
name string The path of the file
n int The number of lines from the bottom to be returned
Examples

String

Get the bottom 10 lines of a file.

load('file', 'tail_lines')
print(tail_lines('path/to/file.txt', 10))
# Output: ['line91', 'line92', ... 'line100']
read_bytes(name)

Reads a file and returns its contents as bytes.

Parameters
name type description
name string The path of the file to be read
Examples

String

Read a file in bytes.

load('file', 'read_bytes')
print(read_bytes('path/to/file.txt'))
# Output: b'file_content'
read_string(name)

Reads a file and returns its contents as string.

Parameters
name type description
name string The path of the file to be read
Examples

String

Read a file in string.

load('file', 'read_string')
print(read_string('path/to/file.txt'))
# Output: 'file_content'
read_lines(name)

Reads a file and returns its contents as a list of lines.

Parameters
name type description
name string The path of the file to be read
Examples

String

Get lines of a file in a list.

load('file', 'read_lines')
print(read_lines('path/to/file.txt'))
# Output: ['line1', 'line2', 'line3', ....]
write_bytes(name, data)

Writes/overwrites bytes or a byte literal string to a file. If the file isn't present, a new file would be created.

Parameters
name type description
name string The path of the file to be written to
data string The byte literal string or bytes to be written to the file
Examples

String

Write a byte string to a file.

load('file', 'write_bytes')
name = 'new_file.txt'
data = b'Hello, This is a new file.'
write_bytes(name, data)
write_string(name, data)

Writes/overwrites a string to a file. If the file isn't present, a new file would be created.

Parameters
name type description
name string The path of the file to be written to
data string The string to be written to the file
Examples

String

Write a string to a file.

load('file', 'write_string')
write_string('new_file.txt', 'Hello, This is a new file.')
write_lines(name, data)

Writes/overwrites a list, tuple or set of lines to a file. If the file isn't present, a new file would be created.

Parameters
name type description
name string The path of the file to be written to
data `list set
Examples

List

Write a list of lines to a file.

load('file', 'write_lines')
lines = ['This is line1', 'This is line2', 'This is line3']
write_lines('new_file.txt', lines)
append_bytes(name, data)

Appends bytes or a byte literal string to a file. If the file isn't present, a new file would be created.

Parameters
name type description
name string The path of the file to be written to
data string The byte literal string or bytes to be appended to the file
Examples

String

Append a byte string to a file.

load('file', 'append_bytes')
append_bytes('existing_file.txt', b'Hello, This is appended data.')
append_string(name, data)

Appends a string to a file. If the file isn't present, a new file would be created.

Parameters
name type description
name string The path of the file to be written to
data string The string to be appended to the file
Examples

String

Append a string to a file.

load('file', 'append_string')
append_string('existing_file.txt', 'Hello, This is appended data.')
append_lines(name, data)

Appends a list, tuple or set of lines to a file. If the file isn't present, a new file would be created.

Parameters
name type description
name string The path of the file to be written to
data `list set
Examples

List

Append a list of lines to a file.

load('file', 'append_lines')
append_lines('existing_file.txt', ['This is line1', 'This is line2', 'This is line3'])

Documentation

Overview

Package file defines functions that manipulate files, it's inspired by file helpers from Amoy.

Index

Constants

View Source
const ModuleName = "file"

ModuleName defines the expected name for this Module when used in starlark's load() function, eg: load('file', 'trim_bom')

Variables

View Source
var (
	// QuitRead indicates the arbitrary error means to quit from reading.
	QuitRead = errors.New("file: quit read by line")
)

Functions

func AppendFileBytes

func AppendFileBytes(path string, data []byte) error

AppendFileBytes writes the given data to the end of a file.

func AppendFileLines

func AppendFileLines(path string, lines []string) error

AppendFileLines appends the given lines to the end of a text file.

func AppendFileString

func AppendFileString(path string, content string) error

AppendFileString appends the given content string to the end of a file.

func CountFileLines

func CountFileLines(path string) (count int, err error)

CountFileLines counts all lines from the given file (the line ending chars are not included).

func LoadModule

func LoadModule() (starlark.StringDict, error)

LoadModule loads the file module. It is concurrency-safe and idempotent.

func ReadFileBytes

func ReadFileBytes(path string) ([]byte, error)

ReadFileBytes reads the whole named file and returns the contents. It's a sugar actually, simply calls os.ReadFile like ioutil.ReadFile does since Go 1.16.

func ReadFileLines

func ReadFileLines(path string) (lines []string, err error)

ReadFileLines reads all lines from the given file (the line ending chars are not included).

func ReadFileString

func ReadFileString(path string) (string, error)

ReadFileString reads the whole named file and returns the contents as a string.

func ReadFirstLines

func ReadFirstLines(path string, n int) (lines []string, err error)

ReadFirstLines reads the top n lines from the given file (the line ending chars are not included), or lesser lines if the given file doesn't contain enough line ending chars.

func ReadLastLines

func ReadLastLines(path string, n int) (lines []string, err error)

ReadLastLines reads the bottom n lines from the given file (the line ending chars are not included), or lesser lines if the given file doesn't contain enough line ending chars.

func TrimUTF8BOM

func TrimUTF8BOM(b []byte) []byte

TrimUTF8BOM removes the leading UTF-8 byte order mark from bytes.

func WriteFileBytes

func WriteFileBytes(path string, data []byte) error

WriteFileBytes writes the given data into a file.

func WriteFileLines

func WriteFileLines(path string, lines []string) error

WriteFileLines writes the given lines as a text file.

func WriteFileString

func WriteFileString(path string, content string) error

WriteFileString writes the given content string into a file.

Types

type LineFunc

type LineFunc func(line string) (err error)

LineFunc stands for a handler for each line string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL