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 nativeglob.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.
Other Versions v: v0.7.4- Tags
- v0.0.1
- v0.1.0
- v0.1.1
- v0.10.0
- v0.10.1
- v0.2.0
- v0.3.0
- v0.3.1
- v0.3.2
- v0.3.3
- v0.4.0
- v0.4.1
- v0.4.2
- v0.4.3
- v0.4.4
- v0.4.5
- v0.4.6
- v0.4.7
- v0.5.0
- v0.5.1
- v0.5.2
- v0.6.0
- v0.6.1
- v0.6.10
- v0.6.11
- v0.6.12
- v0.6.13
- v0.6.14
- v0.6.2
- v0.6.3
- v0.6.4
- v0.6.5
- v0.6.6
- v0.6.7
- v0.6.8
- v0.6.9
- v0.7.0
- v0.7.1
- v0.7.2
- v0.7.3
- v0.7.4
- v0.7.5
- v0.7.6
- v0.8.0
- v0.8.1
- v0.8.2
- v0.8.3
- v0.8.4
- v0.8.5
- v0.8.6
- v0.9.0
- v0.9.1
- v0.9.2
- v0.9.3