hbutils.system.filesystem.directory

Directory and filesystem utilities for copying, removing, and sizing paths.

This module provides Unix-like operations for directories and files, including recursive copy, removal, and size calculation. All public functions accept glob patterns to match multiple files or directories, leveraging hbutils.system.filesystem.file.glob() for pattern expansion.

The module contains the following main components:

  • copy() - Copy files or directories, supporting multiple sources

  • remove() - Remove files or directories with glob support

  • getsize() - Calculate total size of files or directories

Note

These helpers are designed to emulate common Unix commands such as cp -rf, rm -rf, and du -sh.

Example:

>>> from hbutils.system.filesystem.directory import copy, remove, getsize
>>> copy('README.md', 'README.bak')
>>> size = getsize('README.bak')
>>> remove('README.bak')

__all__

hbutils.system.filesystem.directory.__all__ = ['copy', 'remove', 'getsize']

Built-in mutable sequence.

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

copy

hbutils.system.filesystem.directory.copy(src1: str, src2: str, *srcn_dst: str) None[source]

Copy files or directories.

At least two arguments are accepted. When the last path is an existing directory, all preceding paths are copied into that directory. Otherwise, the first path is copied to the last path, and exactly two arguments are accepted in this case; if more sources are provided, a NotADirectoryError is raised.

From Stack Overflow - Copy file or directories recursively in Python.

Parameters:
  • src1 (str) – First source path.

  • src2 (str) – Second source path or destination path.

  • srcn_dst (str) – Additional source paths and the final destination path.

Raises:

NotADirectoryError – If destination is not a directory when multiple sources are provided.

Note

You can use this like cp -rf command on Unix.

Examples::
>>> import os
>>> from hbutils.system import copy
>>>
>>> os.listdir('.')
['test', 'README.md', 'cloc.sh', 'LICENSE', 'codecov.yml', 'pytest.ini', 'Makefile', 'setup.py', 'requirements-test.txt', 'requirements-doc.txt', 'requirements.txt']
>>>
>>> copy('cloc.sh', 'new_cloc.sh')  # copy file
>>> copy('test', 'new_test')  # copy directory
>>> os.listdir('.')
['test', 'README.md', 'cloc.sh', 'LICENSE', 'codecov.yml', 'new_test', 'pytest.ini', 'Makefile', 'setup.py', 'requirements-test.txt', 'requirements-doc.txt', 'requirements.txt', 'new_cloc.sh']
>>>
>>> os.makedirs('new_path_1')
>>> copy('*.txt', 'new_path_1')  # copy to new path
>>> os.listdir('new_path_1')
['requirements-test.txt', 'requirements-doc.txt', 'requirements.txt']
>>>
>>> os.makedirs('new_path_2')
>>> copy('*.txt', 'test/system/**/*.py', 'new_path_2')  # copy plenty of files to new path
>>> print(*os.listdir('new_path_2'), sep='\n')
test_version.py
test_file.py
test_type.py
test_package.py
test_implementation.py
requirements-test.txt
__init__.py
test_directory.py
requirements-doc.txt
requirements.txt

remove

hbutils.system.filesystem.directory.remove(*files: str) None[source]

Remove files or directories.

This function can remove both files and directories. It supports glob patterns to match multiple files at once. The function works recursively for directories.

Parameters:

files (str) – Files or directories to be removed. Supports glob patterns.

Note

You can use this like rm -rf command on Unix.

Examples::
>>> import os
>>> from hbutils.system import remove
>>>
>>> os.listdir('.')
['test', 'README.md', 'cloc.sh', 'codecov.yml', 'new_test', 'new_path_2', 'setup.py', 'requirements-test.txt', 'new_path_1', 'requirements-doc.txt', 'requirements.txt', 'new_cloc.sh']
>>>
>>> remove('codecov.yml')  # remove file
>>> remove('new_test')  # remove directory
>>> os.listdir('.')
['test', 'README.md', 'cloc.sh', 'new_path_2', 'setup.py', 'requirements-test.txt', 'new_path_1', 'requirements-doc.txt', 'requirements.txt', 'new_cloc.sh']
>>>
>>> os.listdir('new_path_1')
['requirements-test.txt', 'requirements-doc.txt', 'requirements.txt']
>>> remove('new_path_1/*.txt')  # remove files from directory
>>> os.listdir('new_path_1')
[]
>>>
>>> print(*os.listdir('new_path_2'), sep='\n')
test_version.py
test_file.py
test_type.py
test_package.py
test_implementation.py
requirements-test.txt
__init__.py
test_directory.py
requirements-doc.txt
requirements.txt
>>> remove('README.md', 'test/**/*.py', 'new_path_2/*.py')  # remove plenty of files
>>> print(*os.listdir('new_path_2'), sep='\n')
requirements-test.txt
requirements-doc.txt
requirements.txt

getsize

hbutils.system.filesystem.directory.getsize(*files: str) int[source]

Get the total size of files or directories.

This function calculates the total size of one or more files or directories. For directories, it recursively sums up all file sizes. Supports glob patterns to match multiple files.

Parameters:

files (str) – File or directory paths. Supports glob patterns.

Returns:

Total size in bytes of all specified files or directories.

Return type:

int

Note

You can use this like du -sh command on Unix.

Examples::
>>> from hbutils.system import getsize
>>>
>>> getsize('README.md')  # a file
5368
>>> getsize('test')  # a directory
1575574
>>> getsize('hbutils/**/*.py')  # glob filtered files
238080