python-fsutil
high-level file-system operations for lazy devs.
Installation
pip install python-fsutil
Usage
Just import the main module and call its methods.
import fsutil
Methods
assert_dir
fsutil.assert_dir(path)
assert_exists
fsutil.assert_exists(path)
assert_file
fsutil.assert_file(path)
assert_not_dir
fsutil.assert_not_dir(path)
assert_not_exists
fsutil.assert_not_exists(path)
assert_not_file
fsutil.assert_not_file(path)
clean_dir
fsutil.clean_dir(path, dirs=True, files=True)
convert_size_bytes_to_string
size_str = fsutil.convert_size_bytes_to_string(size)
convert_size_string_to_bytes
size_bytes = fsutil.convert_size_string_to_bytes(size)
copy_dir
fsutil.copy_dir(path, dest, overwrite=False, **kwargs)
copy_dir_content
fsutil.copy_dir_content(path, dest, **kwargs)
copy_file
fsutil.copy_file(path, dest, overwrite=False, **kwargs)
create_dir
fsutil.create_dir(path, overwrite=False)
create_file
fsutil.create_file(path, content="", overwrite=False)
create_tar_file
fsutil.create_tar_file(path, content_paths, overwrite=True, compression="gzip")
create_zip_file
fsutil.create_zip_file(path, content_paths, overwrite=True, compression=zipfile.ZIP_DEFLATED)
delete_dir
fsutil.delete_dir(path)
delete_dir_content
fsutil.delete_dir_content(path)
delete_dirs
fsutil.delete_dirs(*paths)
delete_file
fsutil.delete_file(path)
delete_files
fsutil.delete_files(*paths)
download_file
filepath = fsutil.download_file(url, dirpath=None, filename="archive.zip", chunk_size=8192, **kwargs)
exists
value = fsutil.exists(path)
fsutil.extract_tar_file(path, dest, content_paths=None, autodelete=False)
fsutil.extract_zip_file(path, dest, content_paths=None, autodelete=False)
get_dir_creation_date
date = fsutil.get_dir_creation_date(path)
get_dir_creation_date_formatted
date_str = fsutil.get_dir_creation_date_formatted(path, format='%Y-%m-%d %H:%M:%S')
get_dir_hash
hash = fsutil.get_dir_hash(path, func="md5")
get_dir_last_modified_date
date = fsutil.get_dir_last_modified_date(path)
get_dir_last_modified_date_formatted
date_str = fsutil.get_dir_last_modified_date_formatted(path, format="%Y-%m-%d %H:%M:%S")
get_dir_size
size = fsutil.get_dir_size(path)
get_dir_size_formatted
size_str = fsutil.get_dir_size_formatted(path)
get_file_basename
basename = fsutil.get_file_basename(path)
get_file_creation_date
date = fsutil.get_file_creation_date(path)
get_file_creation_date_formatted
date_str = fsutil.get_file_creation_date_formatted(path, format="%Y-%m-%d %H:%M:%S")
get_file_extension
extension = fsutil.get_file_extension(path)
get_file_hash
filehash = fsutil.get_file_hash(path, func="md5")
get_file_last_modified_date
date = fsutil.get_file_last_modified_date(path)
get_file_last_modified_date_formatted
date_str = fsutil.get_file_last_modified_date_formatted(path, format="%Y-%m-%d %H:%M:%S")
get_file_size
size = fsutil.get_file_size(path)
get_file_size_formatted
size_str = fsutil.get_file_size_formatted(path)
get_filename
filename = fsutil.get_filename(path)
get_parent_dir
parent_dir = fsutil.get_parent_dir(path, levels=1)
get_permissions
permissions = fsutil.get_permissions(path)
get_unique_name
unique_name = fsutil.get_unique_name(path, prefix="", suffix="", extension="", separator="-")
is_dir
value = fsutil.is_dir(path)
is_empty
value = fsutil.is_empty(path)
is_empty_dir
value = fsutil.is_empty_dir(path)
is_empty_file
value = fsutil.is_empty_file(path)
is_file
value = fsutil.is_file(path)
join_filename
filename = fsutil.join_filename(basename, extension)
join_filepath
filepath = fsutil.join_filepath(dirpath, filename)
join_path
path = fsutil.join_path(path, *paths)
list_dirs
dirs = fsutil.list_dirs(path)
list_files
files = fsutil.list_files(path)
make_dirs
fsutil.make_dirs(path)
make_dirs_for_file
fsutil.make_dirs_for_file(path)
move_dir
fsutil.move_dir(path, dest, overwrite=False, **kwargs)
move_file
fsutil.move_file(path, dest, overwrite=False, **kwargs)
read_file
content = fsutil.read_file(path, encoding="utf-8")
read_file_from_url
content = fsutil.read_file_from_url(url, **kwargs)
read_file_json
data = fsutil.read_file_json(path, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None)
read_file_lines
content = fsutil.read_file_lines(path, line_start=0, line_end=-1, strip_white=True, skip_empty=True, encoding="utf-8")
read_file_lines_count
lines_count = fsutil.read_file_lines_count(path)
remove_dir
fsutil.remove_dir(path, **kwargs)
remove_dir_content
fsutil.remove_dir_content(path)
remove_dirs
fsutil.remove_dirs(*paths)
remove_file
fsutil.remove_file(path)
remove_files
fsutil.remove_files(*paths)
rename_dir
fsutil.rename_dir(path, name)
rename_file
fsutil.rename_file(path, name)
rename_file_basename
fsutil.rename_file_basename(path, basename)
rename_file_extension
fsutil.rename_file_extension(path, extension)
replace_dir
fsutil.replace_dir(path, src, autodelete=False)
replace_file
fsutil.replace_file(path, src, autodelete=False)
search_dirs
dirs = fsutil.search_dirs(path, pattern="**/*")
search_files
files = fsutil.search_files(path, pattern="**/*.*")
set_permissions
fsutil.set_permissions(path, 700)
split_filename
basename, extension = fsutil.split_filename(path)
split_filepath
dirpath, filename = fsutil.split_filepath(path)
split_path
path_names = fsutil.split_path(path)
transform_filepath
filepath = fsutil.transform_filepath(path, dirpath=None, basename=lambda b: slugify(b), extension="webp")
write_file
fsutil.write_file(path, content, append=False, encoding="utf-8", atomic=False)
write_file_json
fsutil.write_file_json(path, data, encoding="utf-8", atomic=False, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False)
Testing
git clone https://github.com/fabiocaccamo/python-fsutil.git && cd python-fsutil
python -m venv venv && . venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -r requirements.txt -r requirements-test.txt
pre-commit install --install-hooks
tox
python -m unittest
License
Released under MIT License.
Supporting
- :star: Star this project on GitHub
- :octocat: Follow me on GitHub
- :blue_heart: Follow me on Twitter
- :moneybag: Sponsor me on Github
See also
-
python-benedict
- dict subclass with keylist/keypath support, I/O shortcuts (base64, csv, json, pickle, plist, query-string, toml, xml, yaml) and many utilities. 📘
-
python-fontbro
- friendly font operations on top of fontTools. 🧢