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.
- class cape.fileutils.LatestRegex(fname, regmatch)¶
Output type containing latest file name and regex match instance
- fname¶
Alias for field number 0
- regmatch¶
Alias for field number 1
- cape.fileutils.count_lines(fname: str) int ¶
Count lines in a file
Meant to mimic results of
$ wc -l $FNAME
but written in pure Python.
- cape.fileutils.get_latest_file(filelist: list) str ¶
Get the latest modified file from a list of files
- cape.fileutils.get_latest_regex(pat: str, baseglob: str | None = None) LatestRegex ¶
Get the latest modified file matching a regular expression
- Call:
>>> latest = get_latest_regex(pat, baseglob=None)
- Inputs:
- Outputs:
- latest:
LatestRegex
Regex file-search instance
- latest.fname:
str
Name of latest modified file in list
- latest.regmatch:
re.Match
Regular expression groups, etc. for fname
- latest:
- Version:
2024-03-24
@ddalle
: v1.02025-02-04
@ddalle
: v1.1; add special output type
- cape.fileutils.grep(pat: str, fname: str, nmax: int | None = None, encoding: str = 'utf-8') list ¶
Find lines of a file containing a regular expressoin
- Call:
>>> lines = grep(pat, fname, nmax=None, encoding="utf-8")
- Inputs:
- Outputs:
- Versions:
2023-06-16
@ddalle
: v1.02024-07-30
@ddalle
: v1.1; add nmax
- cape.fileutils.readline_reverse(fb: IOBase) bytes ¶
Read line ending at current position
- Call:
>>> txt = readline_reverse(fb)
- Inputs:
- fb:
IOBase
File handle open for reading in binary mode
- fb:
- Outputs:
- txt:
bytes
Encoded text of last line
- txt:
- Versions:
2024-07-26
@ddalle
: v1.0
- cape.fileutils.sort_by_mtime(filelist: list) list ¶
Sort list of files by modification time (ascending)