pygnuutils
pygnuutils is a pure Python implementation for various GNU core utilities, aiming to provide similar functionality in a Pythonic way. It is currently at version 0.1.1 and has a moderate release cadence, with the last update over a year ago.
Common errors
-
ModuleNotFoundError: No module named 'pygnuutils'
cause The `pygnuutils` package is not installed in the active Python environment.fixRun `pip install pygnuutils` to install the library. -
TypeError: Ls() got an unexpected keyword argument 'some_flag'
cause You are attempting to pass a command-line style flag (e.g., `-R`) directly as a keyword argument, but the Python wrapper expects a specific Pythonic argument name (e.g., `recursive=True`).fixCheck the `pygnuutils` source code or documentation for the specific utility to find the correct Python keyword argument. For `Ls`, common arguments include `all_`, `long`, `recursive`, etc.
Warnings
- gotcha pygnuutils is a pure Python re-implementation. It may not offer full feature parity, exact behavioral equivalence, or the same performance as the native C-based GNU utilities, especially for edge cases or large-scale I/O.
- gotcha Command-line arguments are translated to Pythonic keyword arguments (e.g., `-a` becomes `all_=True`, `-l` becomes `long=True`). The exact mapping might not be immediately obvious or cover all native GNU options.
Install
-
pip install pygnuutils
Imports
- Ls
from pygnuutils import ls
from pygnuutils.ls import Ls
- Basename
from pygnuutils.basename import Basename
Quickstart
from pygnuutils.ls import Ls
# Instantiate the Ls utility
ls = Ls()
# List contents of the current directory, similar to 'ls -a'
print("Current directory (ls -a):")
ls('.', all_=True) # Note: 'all_' for '-a' argument
# List contents of a specific directory in long format, similar to 'ls -l /tmp'
print("\n/tmp directory (ls -l):")
ls('/tmp', long=True)
# Example with dependency injection (a key feature mentioned in README)
import os
class ReadlinkWatch(Ls.stub):
def readlink(self, path, dir_fd=None):
print(f'Resolving symlink: {path}...')
return os.readlink(path, dir_fd=dir_fd)
ls_with_watch = Ls(stub=ReadlinkWatch())
print("\nListing with symlink watcher:")
# This might require creating a symlink in /tmp for demonstration
# Example: os.symlink('/etc/passwd', '/tmp/mylink')
# For safe execution, we won't create it here, but demonstrate the call.
try:
ls_with_watch('/tmp', long=True)
except OSError as e:
print(f"Could not complete symlink watch example: {e}")
print("To run, ensure you have symlinks in /tmp or create one, e.g., 'os.symlink('/etc/passwd', '/tmp/mylink')'")