为懒散的开发者提供高级文件系统操作。
项目描述
python-fsutil
为懒散的开发者提供高级文件系统操作。
安装
pip install python-fsutil
使用
只需导入主模块并调用其方法。
import fsutil
方法
assert_dir
assert_exists
断言文件
断言非目录
断言不存在
断言非文件
清理目录
将字节大小转换为字符串
将字符串大小转换为字节
复制目录
复制目录内容
复制文件
创建目录
创建文件
创建tar文件
创建zip文件
删除目录
删除目录内容
删除目录们
删除文件
删除文件们
download_file
(需要安装requests
)存在
解压tar文件
解压zip文件
获取目录创建日期
获取格式化目录创建日期
获取目录哈希
获取目录最后修改日期
获取格式化目录最后修改日期
获取目录大小
获取格式化目录大小
获取文件基本名
获取文件创建日期
获取格式化文件创建日期
获取文件扩展名
获取文件哈希
获取文件最后修改日期
获取格式化文件最后修改日期
获取文件大小
获取格式化文件大小
获取文件名
获取父目录
获取权限
获取唯一名称
是否为目录
是否为空
是否为空目录
是否为空文件
是否为文件
连接文件名
连接文件路径
连接路径
列出目录
列出文件
创建目录
为文件创建目录
移动目录
移动文件
读取文件
read_file_from_url
(需要安装requests
)读取文件JSON
读取文件行
读取文件行数
移除目录
移除目录内容
移除目录们
移除文件
移除文件们
重命名目录
重命名文件
重命名文件基本名
重命名文件扩展名
替换目录
替换文件
搜索目录
搜索文件
设置权限
分割文件名
分割文件路径
分割路径
转换文件路径
写入文件
写入文件JSON
assert_dir
# Raise an OSError if the given path doesn't exist or it is not a directory.
fsutil.assert_dir(path)
assert_exists
# Raise an OSError if the given path doesn't exist.
fsutil.assert_exists(path)
断言文件
# Raise an OSError if the given path doesn't exist or it is not a file.
fsutil.assert_file(path)
断言非目录
# Raise an OSError if the given path is an existing directory.
fsutil.assert_not_dir(path)
断言不存在
# Raise an OSError if the given path already exists.
fsutil.assert_not_exists(path)
断言非文件
# Raise an OSError if the given path is an existing file.
fsutil.assert_not_file(path)
清理目录
# Clean a directory by removing empty sub-directories and/or empty files.
fsutil.clean_dir(path, dirs=True, files=True)
将字节大小转换为字符串
# Convert the given size bytes to string using the right unit suffix.
size_str = fsutil.convert_size_bytes_to_string(size)
将字符串大小转换为字节
# Convert the given size string to bytes.
size_bytes = fsutil.convert_size_string_to_bytes(size)
复制目录
# Copy the directory at the given path and all its content to dest path.
# If overwrite is not allowed and dest path exists, an OSError is raised.
# More informations about kwargs supported options here:
# https://docs.pythonlang.cn/3/library/shutil.html#shutil.copytree
fsutil.copy_dir(path, dest, overwrite=False, **kwargs)
复制目录内容
# Copy the content of the directory at the given path to dest path.
# More informations about kwargs supported options here:
# https://docs.pythonlang.cn/3/library/shutil.html#shutil.copytree
fsutil.copy_dir_content(path, dest, **kwargs)
复制文件
# Copy the file at the given path and its metadata to dest path.
# If overwrite is not allowed and dest path exists, an OSError is raised.
# More informations about kwargs supported options here:
# https://docs.pythonlang.cn/3/library/shutil.html#shutil.copy2
fsutil.copy_file(path, dest, overwrite=False, **kwargs)
创建目录
# Create directory at the given path.
# If overwrite is not allowed and path exists, an OSError is raised.
fsutil.create_dir(path, overwrite=False)
创建文件
# Create file with the specified content at the given path.
# If overwrite is not allowed and path exists, an OSError is raised.
fsutil.create_file(path, content="", overwrite=False)
创建tar文件
# Create tar file at path compressing directories/files listed in content_paths.
# If overwrite is allowed and dest tar already exists, it will be overwritten.
fsutil.create_tar_file(path, content_paths, overwrite=True, compression="gzip")
创建zip文件
# Create zip file at path compressing directories/files listed in content_paths.
# If overwrite is allowed and dest zip already exists, it will be overwritten.
fsutil.create_zip_file(path, content_paths, overwrite=True, compression=zipfile.ZIP_DEFLATED)
删除目录
# Alias for remove_dir.
fsutil.delete_dir(path)
删除目录内容
# Alias for remove_dir_content.
fsutil.delete_dir_content(path)
删除目录们
# Alias for remove_dirs.
fsutil.delete_dirs(*paths)
删除文件
# Alias for remove_file.
fsutil.delete_file(path)
删除文件们
# Alias for remove_files.
fsutil.delete_files(*paths)
download_file
# Download a file from url to the given dirpath and return the filepath.
# If dirpath is not provided, the file will be downloaded to a temp directory.
# If filename is provided, the file will be named using filename.
# It is possible to pass extra request options (eg. for authentication) using **kwargs.
filepath = fsutil.download_file(url, dirpath=None, filename="archive.zip", chunk_size=8192, **kwargs)
存在
# Check if a directory of a file exists at the given path.
value = fsutil.exists(path)
解压tar文件
# Extract tar file at path to dest path.
# If autodelete, the archive will be deleted after extraction.
# If content_paths list is defined, only listed items will be extracted, otherwise all.
fsutil.extract_tar_file(path, dest, content_paths=None, autodelete=False)
解压zip文件
# Extract zip file at path to dest path.
# If autodelete, the archive will be deleted after extraction.
# If content_paths list is defined, only listed items will be extracted, otherwise all.
fsutil.extract_zip_file(path, dest, content_paths=None, autodelete=False)
获取目录创建日期
# Get the directory creation date.
date = fsutil.get_dir_creation_date(path)
获取格式化目录创建日期
# Get the directory creation date formatted using the given format.
date_str = fsutil.get_dir_creation_date_formatted(path, format='%Y-%m-%d %H:%M:%S')
获取目录哈希
# Get the hash of the directory at the given path using
# the specified algorithm function (md5 by default).
hash = fsutil.get_dir_hash(path, func="md5")
获取目录最后修改日期
# Get the directory last modification date.
date = fsutil.get_dir_last_modified_date(path)
获取格式化目录最后修改日期
# Get the directory last modification date formatted using the given format.
date_str = fsutil.get_dir_last_modified_date_formatted(path, format="%Y-%m-%d %H:%M:%S")
获取目录大小
# Get the directory size in bytes.
size = fsutil.get_dir_size(path)
获取格式化目录大小
# Get the directory size formatted using the right unit suffix.
size_str = fsutil.get_dir_size_formatted(path)
获取文件基本名
# Get the file basename from the given path/url.
basename = fsutil.get_file_basename(path)
获取文件创建日期
# Get the file creation date.
date = fsutil.get_file_creation_date(path)
获取格式化文件创建日期
# Get the file creation date formatted using the given format.
date_str = fsutil.get_file_creation_date_formatted(path, format="%Y-%m-%d %H:%M:%S")
获取文件扩展名
# Get the file extension from the given path/url.
extension = fsutil.get_file_extension(path)
获取文件哈希
# Get the hash of the file at the given path using
# the specified algorithm function (md5 by default).
filehash = fsutil.get_file_hash(path, func="md5")
获取文件最后修改日期
# Get the file last modification date.
date = fsutil.get_file_last_modified_date(path)
获取格式化文件最后修改日期
# Get the file last modification date formatted using the given format.
date_str = fsutil.get_file_last_modified_date_formatted(path, format="%Y-%m-%d %H:%M:%S")
获取文件大小
# Get the file size in bytes.
size = fsutil.get_file_size(path)
获取格式化文件大小
# Get the file size formatted using the right unit suffix.
size_str = fsutil.get_file_size_formatted(path)
获取文件名
# Get the filename from the given path/url.
filename = fsutil.get_filename(path)
获取父目录
# Get the parent directory for the given path going up N levels.
parent_dir = fsutil.get_parent_dir(path, levels=1)
获取权限
# Get the file/directory permissions.
permissions = fsutil.get_permissions(path)
获取唯一名称
# Get a unique name for a directory/file ath the given directory path.
unique_name = fsutil.get_unique_name(path, prefix="", suffix="", extension="", separator="-")
是否为目录
# Determine whether the specified path represents an existing directory.
value = fsutil.is_dir(path)
是否为空
# Determine whether the specified path represents an empty directory or an empty file.
value = fsutil.is_empty(path)
是否为空目录
# Determine whether the specified path represents an empty directory.
value = fsutil.is_empty_dir(path)
是否为空文件
# Determine whether the specified path represents an empty file.
value = fsutil.is_empty_file(path)
是否为文件
# Determine whether the specified path represents an existing file.
value = fsutil.is_file(path)
连接文件名
# Create a filename joining the file basename and the extension.
filename = fsutil.join_filename(basename, extension)
连接文件路径
# Create a filepath joining the directory path and the filename.
filepath = fsutil.join_filepath(dirpath, filename)
连接路径
# Create a path joining path and paths.
# If path is __file__ (or a .py file), the resulting path will be relative
# to the directory path of the module in which it's used.
path = fsutil.join_path(path, *paths)
列出目录
# List all directories contained at the given directory path.
dirs = fsutil.list_dirs(path)
列出文件
# List all files contained at the given directory path.
files = fsutil.list_files(path)
创建目录
# Create the directories needed to ensure that the given path exists.
# If a file already exists at the given path an OSError is raised.
fsutil.make_dirs(path)
为文件创建目录
# Create the directories needed to ensure that the given path exists.
# If a directory already exists at the given path an OSError is raised.
fsutil.make_dirs_for_file(path)
移动目录
# Move an existing dir from path to dest directory.
# If overwrite is not allowed and dest path exists, an OSError is raised.
# More informations about kwargs supported options here:
# https://docs.pythonlang.cn/3/library/shutil.html#shutil.move
fsutil.move_dir(path, dest, overwrite=False, **kwargs)
移动文件
# Move an existing file from path to dest directory.
# If overwrite is not allowed and dest path exists, an OSError is raised.
# More informations about kwargs supported options here:
# https://docs.pythonlang.cn/3/library/shutil.html#shutil.move
fsutil.move_file(path, dest, overwrite=False, **kwargs)
读取文件
# Read the content of the file at the given path using the specified encoding.
content = fsutil.read_file(path, encoding="utf-8")
read_file_from_url
# Read the content of the file at the given url.
content = fsutil.read_file_from_url(url, **kwargs)
读取文件JSON
# Read and decode a json encoded file at the given path.
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 content lines.
# It is possible to specify the line indexes (negative indexes too),
# very useful especially when reading large files.
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 a directory at the given path and all its content.
# If the directory is removed with success returns True, otherwise False.
# More informations about kwargs supported options here:
# https://docs.pythonlang.cn/3/library/shutil.html#shutil.rmtree
fsutil.remove_dir(path, **kwargs)
移除目录内容
# Removes all directory content (both sub-directories and files).
fsutil.remove_dir_content(path)
移除目录们
# Remove multiple directories at the given paths and all their content.
fsutil.remove_dirs(*paths)
移除文件
# Remove a file at the given path.
# If the file is removed with success returns True, otherwise False.
fsutil.remove_file(path)
移除文件们
# Remove multiple files at the given paths.
fsutil.remove_files(*paths)
重命名目录
# Rename a directory with the given name.
# If a directory or a file with the given name already exists, an OSError is raised.
fsutil.rename_dir(path, name)
重命名文件
# Rename a file with the given name.
# If a directory or a file with the given name already exists, an OSError is raised.
fsutil.rename_file(path, name)
重命名文件基本名
# Rename a file basename with the given basename.
fsutil.rename_file_basename(path, basename)
重命名文件扩展名
# Rename a file extension with the given extension.
fsutil.rename_file_extension(path, extension)
替换目录
# Replace directory at the specified path with the directory located at src.
# If autodelete, the src directory will be removed at the end of the operation.
# Optimized for large directories.
fsutil.replace_dir(path, src, autodelete=False)
替换文件
# Replace file at the specified path with the file located at src.
# If autodelete, the src file will be removed at the end of the operation.
# Optimized for large files.
fsutil.replace_file(path, src, autodelete=False)
搜索目录
# Search for directories at path matching the given pattern.
dirs = fsutil.search_dirs(path, pattern="**/*")
搜索文件
# Search for files at path matching the given pattern.
files = fsutil.search_files(path, pattern="**/*.*")
设置权限
# Set the file/directory permissions.
fsutil.set_permissions(path, 700)
分割文件名
# Split a filename and returns its basename and extension.
basename, extension = fsutil.split_filename(path)
分割文件路径
# Split a filename and returns its directory-path and filename.
dirpath, filename = fsutil.split_filepath(path)
分割路径
# Split a path and returns its path-names.
path_names = fsutil.split_path(path)
转换文件路径
# Trasform a filepath by applying the provided optional changes.
filepath = fsutil.transform_filepath(path, dirpath=None, basename=lambda b: slugify(b), extension="webp")
写入文件
# Write file with the specified content at the given path.
fsutil.write_file(path, content, append=False, encoding="utf-8", atomic=False)
写入文件JSON
# Write a json file at the given path with the specified data encoded in json format.
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)
测试
# clone repository
git clone https://github.com/fabiocaccamo/python-fsutil.git && cd python-fsutil
# create virtualenv and activate it
python -m venv venv && . venv/bin/activate
# upgrade pip
python -m pip install --upgrade pip
# install requirements
python -m pip install -r requirements.txt -r requirements-test.txt
# install pre-commit to run formatters and linters
pre-commit install --install-hooks
# run tests using tox
tox
# or run tests using unittest
python -m unittest
许可
在MIT许可证下发布。
支持
另请参阅
-
python-benedict
- 支持键列表/键路径支持的dict子类,I/O快捷方式(base64、csv、json、pickle、plist、查询字符串、toml、xml、yaml)和许多实用工具。 📘 -
python-fontbro
- 在fontTools之上进行友好的字体操作。 🧢
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源分发
python-fsutil-0.14.1.tar.gz (26.7 kB 查看哈希)
构建分发
python_fsutil-0.14.1-py3-none-any.whl (16.1 kB 查看哈希值)
关闭
python-fsutil-0.14.1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 8fb204fa8059f37bdeee8a1dc0fff010170202ea47c4225ee71bb3c26f3997be |
|
MD5 | edae6a3316fcd42989173c8eb3ec53d1 |
|
BLAKE2b-256 | de17e7426ac2d9bad932bd6f893781c66c042660d92bec57adb50f36dc7380d5 |
关闭
python_fsutil-0.14.1-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 0d45e623f0f4403f674bdd8ae7aa7d24a4b3132ea45c65416bd2865e6b20b035 |
|
MD5 | 97fabbdb59038fcbfb9ed19b94c2b0a7 |
|
BLAKE2b-256 | 4fe6c8a2cdf34316bb96a7601ed47778818da266d4917bd9575d3a9ba46aedb7 |