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 |
|---|---|---|
|
|
Examples
basic
Removes the Byte Order Mark (BOM) from a string.
count_lines(name) int
Counts the total lines in a file located at the provided path.
Parameters
name | type | description |
|---|---|---|
|
| The path of the file whose lines are to be counted |
Examples
basic
Count the lines of a file.
head_lines(name, n) []string
Returns the first 'n' lines of a file.
Parameters
name | type | description |
|---|---|---|
|
| The path of the file |
|
| The number of lines from the top to be returned |
Examples
basic
Get the top 10 lines of a file.
tail_lines(name, n) []string
Returns the last 'n' lines of a file.
Parameters
name | type | description |
|---|---|---|
|
| The path of the file |
|
| The number of lines from the bottom to be returned |
Examples
basic
Get the bottom 10 lines of a file.
read_bytes(name)
Reads a file and returns its contents as bytes.
Parameters
name | type | description |
|---|---|---|
|
| The path of the file to be read |
Examples
basic
Read a file in bytes.
read_string(name)
Reads a file and returns its contents as string.
Parameters
name | type | description |
|---|---|---|
|
| The path of the file to be read |
Examples
basic
Read a file in string.
read_lines(name)
Reads a file and returns its contents as a list of lines.
Parameters
name | type | description |
|---|---|---|
|
| The path of the file to be read |
Examples
basic
Get lines of a file in a list.
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 |
|---|---|---|
|
| The path of the file to be written to |
|
| The byte literal string or bytes to be written to the file |
Examples
basic
Write a byte string to a file.
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 |
|---|---|---|
|
| The path of the file to be written to |
|
| The string to be written to the file |
Examples
basic
Write a string to a 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 |
|---|---|---|
|
| The path of the file to be written to |
| `list | set |
Examples
List
Write a list of lines to a file.
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 |
|---|---|---|
|
| The path of the file to be written to |
|
| The byte literal string or bytes to be appended to the file |
Examples
basic
Append a byte string to a file.
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 |
|---|---|---|
|
| The path of the file to be written to |
|
| The string to be appended to the file |
Examples
basic
Append a string to a file.
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 |
|---|---|---|
|
| The path of the file to be written to |
| `list | set |
Examples
basic
Append a list of lines to a file.
stat(name, follow=False) FileStat
Returns a FileStat object representing information about the given file or directory.
Parameters
name | type | description |
|---|---|---|
|
| The path of the file or directory. |
|
| If true, symbolic links are followed. |
Examples
file information
Retrieve information about a file.
directory information
Retrieve information about a directory.
copyfile(src, dst, overwrite=False) string
Copies a file from source to destination, and returns the destination file path. If the destination exists and overwrite is set to False, an error is returned. If the destination is a directory, the file is copied into that directory with its original filename. Symbolic links are followed. Mode, access, and modification times are preserved.
Parameters
name | type | description |
|---|---|---|
|
| The path of the source file to be copied. |
|
| The path of the destination file or directory. The parent directory must exist. |
|
| If true, allows overwriting the destination file if it exists. Defaults to False. |
Examples
basic copy
Copy a file to a new location without overwrite.
overwrite copy
Copy a file to a new location with overwrite enabled.
copy to directory
Copy a file into a directory.
Types
FileStat
Represents information about a file.
Fields
name | type | description |
|---|---|---|
|
| The name of the file. |
|
| The full path of the file. |
|
| The file extension. |
|
| The size of the file in bytes. |
|
| The type of the file: |
|
| The last modified time of the file. |
|
| Returns the MD5 hash of the file contents. |
|
| Returns the SHA-1 hash of the file contents. |
|
| Returns the SHA-256 hash of the file contents. |
|
| Returns the SHA-512 hash of the file contents. |