hbutils.system.filesystem.file

File system utilities for creating files and performing glob-based searches.

This module provides lightweight helpers that wrap common filesystem operations, including file creation (similar to the Unix touch command) and pattern-based file discovery using glob expressions.

The module contains the following public components:

  • touch() - Create or update files, optionally creating parent directories.

  • glob() - Iterate over file paths that match one or more glob patterns.

Note

The glob() helper yields results as a generator, which is more memory-efficient than glob.glob() for large result sets.

Example:

>>> from hbutils.system.filesystem.file import touch, glob
>>> touch('data/output.txt')
>>> list(glob('data/*.txt'))
['data/output.txt']

__all__

hbutils.system.filesystem.file.__all__ = ['touch', 'glob']

Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

touch

hbutils.system.filesystem.file.touch(file: str, exist_ok: bool = True, makedirs: bool = True) None[source]

Touch the file at given path.

This function creates an empty file if it does not exist, or updates its modification time if it does. It can also create parent directories automatically.

Parameters:
  • file (str) – Path of the file to create or update.

  • exist_ok (bool) – If True, do not raise an error if the file already exists.

  • makedirs (bool) – If True, create parent directories as needed.

Returns:

This function returns None.

Return type:

None

Note

You can use this like the Unix touch command.

Example:

>>> import os
>>> from hbutils.system import touch
>>> os.listdir('.')
[]
>>> touch('simple.txt')  # touch simple file
>>> touch('1/2/3/simple.txt')  # touch file in nested directory (1/2/3 will be created)
>>> os.listdir('.')
['1', 'simple.txt']
>>> os.listdir('1/2/3')
['simple.txt']

glob

hbutils.system.filesystem.file.glob(*items: str) Iterator[str][source]

Glob filter by the given items.

This function performs pattern matching on file paths using glob patterns. Unlike the native glob.glob(), this function returns a generator that yields matching paths, making it more memory-efficient for large result sets.

Parameters:

items (str) – One or more glob patterns to match against file paths.

Returns:

Generator yielding paths that match any of the provided patterns.

Return type:

Iterator[str]

Note

glob() is different from native glob.glob(), for its return value is a generator instead of a list.

Example:

>>> from hbutils.system import glob
>>>
>>> list(glob('*.md'))  # simple filter
['CONTRIBUTING.md', 'README.md']
>>> list(glob('*.md', '*.txt'))  # multiple filter
['CONTRIBUTING.md', 'README.md', 'requirements-test.txt', 'requirements-doc.txt', 'requirements.txt']
>>> print(*glob('hbutils/system/**/*.py'), sep='\n')  # nested filter
hbutils/system/__init__.py
hbutils/system/filesystem/directory.py
hbutils/system/filesystem/file.py
hbutils/system/filesystem/__init__.py
hbutils/system/python/package.py
hbutils/system/python/implementation.py
hbutils/system/python/version.py
hbutils/system/python/__init__.py
hbutils/system/os/type.py
hbutils/system/os/__init__.py