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.get_latest_file(filelist: list) str

Get the latest modified file from a list of files

Call:
>>> fname = get_latest_file(filelist)
Inputs:
filelist: list[str]

List of file names

Outputs:
fname: str

Name of latest modified file in list

Version:
  • 2024-01-21 @ddalle: v1.0

cape.fileutils.get_latest_regex(pat: str, baseglob=None)

Get the latest modified file matching a regular expression

Call:
>>> fname, regmatch = get_latest_regex(pat, baseglob=None)
Inputs:
pat: str

Regular expression string

baseglob: {None} | str

Optional glob pattern to narrow candidates in current dir

Outputs:
fname: str

Name of latest modified file in list

regmatch: re.Match

Regular expression groups, etc. for fname

Version:
  • 2024-03-24 @ddalle: v1.0

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.sort_by_mtime(filelist: list) list

Sort list of files by modification time (ascending)

Call:
>>> tfiles = sort_by_mtime(filelist)
Inputs:
filelist: list[str]

List of file names

Outputs:
tfiles: list[str]

Files of filelist in order of ascending modification time

Version:
  • 2024-01-21 @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

Outputs:
txt: str

Text of last n lines of fname

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