cape.fileutils: Pure-Python file information utilities

This module provides several file utilities that mimic common BASH commands or programs, but written in pure Python to remove operating system dependencies.

cape.fileutils.grep(pat: str, fname: str, encoding='utf-8') list

Find lines of a file containing a regular expressoin

Call:
>>> lines = grep(pat, fname, encoding="utf-8")
Inputs:
pat: str

String of regular expression pattern

fname: str

Name of file to search

encoding: {"utf-8"} | str

Encoding for file

Outputs:
lines: list[str]

List of lines containing a match of pat

Versions:
  • 2023-06-16 @ddalle: v1.0

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

Get first n lines of a file

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

Name of file to read

n: {1} | int

Number of lines to read from beginning of file

encoding: {"utf-8"} | str

Encoding for text file

Versions:
  • 2023-06-16 @ddalle: v1.0

cape.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

Versions:
  • 2023-06-16 @ddalle: v1.0

cape.fileutils.touch(fname: str)

Replicate touch; creating new file or updating mod time

Call:
>>> touch(fname)
Inputs:
fname: str

Name of file to create/modify

Versions:
  • 2023-06-16 @ddalle: v1.0