Filesystem#
Things to do with the filesystem.
- ktz.filesystem.path(name, create=False, exists=False, is_dir=None, is_file=None)#
Create paths.
This simply gathers usual operations on Path objects for which normally multiple calls to the pathlib are required.
- Parameters:
- namestr | pathlib.Path
The path in question
- createbool
Whether to create a directory if it does not exist
- existsbool
Check if the path exists, otherwise raise
- is_dirbool | None
Check if path is a directory, otherwise raise
- is_filebool | None
Check if path is a file, otherwise raise
- Returns:
- pathlib.Path
A Path instance
- Raises:
- ktz.Error
Raised if any of the constraints are violated
Examples
>>> from ktz.filesystem import path >>> somedir = path('foo/bar', create=True) >>> path(somedir, exists=True) PosixPath('foo/bar') >>> path(somedir, is_dir=True) PosixPath('foo/bar') >>> path(somedir, is_file=True) Traceback (most recent call last): (...) Error: foo/bar exists but is not a file
- ktz.filesystem.path_rotate(current, keep=None)#
Rotate a file.
Given a file “foo.tar”, rotating it will produce “foo.1.tar”. If “foo.1.tar” already exists then “foo.1.tar” -> “foo.2.tar”. And so on. Also works for directories.
If ‘keep’ is set to a positive integer, keeps at most that much files.
- Parameters:
- currentstr | pathlib.Path
Target file or directory
- keepint
How many rotated files to keep at most
Examples
>>> from ktz.filesystem import path_rotate >>> ! touch test.txt >>> path_rotate('test.txt', keep=2) >>> ! ls test.1.txt >>> ! touch test.txt >>> ! ls test.txt test.1.txt >>> path_rotate('test.txt', keep=2) >>> ! ls test.1.txt test.2.txt >>> ! touch test.txt >>> ! ls test.txt test.1.txt test.2.txt >>> path_rotate('test.txt', keep=2) >>> ! ls test.1.txt test.2.txt