hbutils.system.filesystem

Overview:

Utilities for filesystem.

touch

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

Touch the file at given path. Just like the touch command in unix system.

Parameters:
  • file – Path of the file.

  • exist_ok – Exist is okay or not.

  • makedirs – Create directories when necessary.

Note

You can use this like touch command on unix.

Examples::
>>> 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.glob(*items) → Iterator[str][source]
Overview:

Glob filter by the given items.

Parameters:

items – Filter items.

Returns:

Filtered existing paths.

Note

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

Examples::
>>> 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

copy

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

Copy files or directories.

No less than 2 arguments are accepted. When the last path is an existing path, all the fore paths will be copied to this path. Otherwise, the first path will be copied to the last path (exactly 2 arguments are accepted in this case, or NotADirectoryError will be raised).

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

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.remove(*files: str)[source]
Overview:

Remove a file or a directory at file. file can be a file or a directory, both are supported.

Parameters:

files – Files or directories to be removed.

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.getsize(*files: str)[source]
Overview:

Get size of a file or a directory.

Parameters:

files – File paths.

Returns:

Size of the file or the total size of the directory.

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

is_binary_file

hbutils.system.filesystem.is_binary_file(filename) → bool[source]
Overview:

Check if the given file is binary file.

Parameters:

filename – Filename.

Returns:

Is binary file or not.

Examples::
>>> from hbutils.system import is_binary_file
>>> is_binary_file('rar_template-simple.rar')
True
>>> is_binary_file('README.md')
False

Note

Empty file will be seen as text file.

is_text_file

hbutils.system.filesystem.is_text_file(filename) → bool[source]
Overview:

Check if the given file is text file.

Parameters:

filename – Filename.

Returns:

Is text file or not.

Examples::
>>> from hbutils.system import is_text_file
>>> is_text_file('rar_template-simple.rar')
False
>>> is_text_file('README.md')
True

Note

Empty file will be seen as text file.

TemporaryDirectory

class hbutils.system.filesystem.TemporaryDirectory(suffix=None, prefix=None, dir=None)[source]

Create and return a temporary directory. This has the same behavior as mkdtemp but can be used as a context manager. For example:

with TemporaryDirectory() as tmpdir:

Upon exiting the context, the directory and everything contained in it are removed.