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.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=None)¶
Get the latest modified file matching a regular expression
- Call:
>>> fname, regmatch = get_latest_regex(pat, baseglob=None)
- Inputs:
- Outputs:
- fname:
str
Name of latest modified file in list
- regmatch:
re.Match
Regular expression groups, etc. for fname
- fname:
- Version:
2024-03-24
@ddalle
: v1.0
- 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)