cape.gruvoc.fileutils: Common file analysis tools

This module provides functions for opening or analyzing files.

cape.gruvoc.fileutils.isfile(fp) → bool

Check if a variable is a file handle

Class:
>>> q = isfile(fp)
Inputs:
fp: file | any

Any variable, usually file handle or file name

Outputs:
q: True | False

Whether or not fp is a file handle

cape.gruvoc.fileutils.keep_pos(func)
cape.gruvoc.fileutils.openfile(fname_or_fp: str | IOBase, mode: str = 'rb')

Process a file handle or name and save attributes

Call:
>>> fb = openfile(fname, mode="rb")
>>> fb = openfile(fp, mode="rb")
>>> fb = openfile(fb, mode="rb")
>>> fb = openfile(None, mode="rb")
Inputs:
fname: str

Name of a file to read

fp: file

Existing file handle open in ‘r’ mode

fb: file

Existing file handle open in ‘rb’ mode

mode: {"rb"} | str

File read/write mode

Outputs:
fp: io.IOBase

File handle open in mode mode

cape.gruvoc.fileutils.tail(fname: str, n=1, encoding='utf-8')

Get last n lines of a file

Call:
>>> txt = tail(fname, n=1)
Inputs:
fname: str

Name of file to read

n: {1} | int

Number of lines to read from end of file

encoding: {"utf-8"} | str

Encoding for text file

Outputs:
txt: str

Text of last n lines of fname