Skip to main content

funfile

一个实用的 Python 文件操作工具库,提供带进度条的压缩/解压、并发写入、pickle 序列化等功能的增强封装。

nltfile 已迁回 funfile;旧包保留兼容入口,新代码请使用 funfile

特性

  • 带进度条的 tar 压缩与解压,用法与标准库一致
  • 自动识别常见 zip/tar 格式,并阻止 tar 路径穿越
  • 线程安全的后台文件写入,支持指定偏移量
  • pickle 序列化/反序列化的便捷封装
  • 常用文件系统操作(创建目录、删除、复制)

安装

pip install funfile

依赖

使用

tar 压缩/解压(带进度条)

用法与标准库 tarfile 一致,自动显示进度条:

from funfile import tarfile

# 压缩
with tarfile.open("results.tar.xz", "w|xz") as tar:
    tar.add("a.txt")

# 解压
with tarfile.open("results.tar.xz", "r|xz") as tar:
    tar.extractall("local")

也可以使用快捷函数:

from funfile.compress.tarfile import file_entar, file_detar

# 一键压缩
file_entar("mydir", "mydir.tar.xz")

# 一键解压
file_detar("mydir.tar.xz", "output")

zip 解压

from funfile import zipfile

with zipfile.ZipFile("archive.zip") as zf:
    zf.extractall("output")

通用解压

根据文件后缀自动选择解压方式,支持 .zip.tar.tar.gz.tgz.tar.bz2.tar.xz.txz。tar 解压默认只接受普通文件和目录,拒绝目标目录之外的路径及链接、设备等特殊成员:

from funfile.compress.allfile import extractall

extractall("archive.tar.gz", "output")

并发文件写入

线程安全的后台写入,适用于多线程生产数据的场景。离开 with 时会等待数据落盘,后台写入错误会传播给调用方:

from funfile import ConcurrentFile

with ConcurrentFile("output.txt", mode="w") as fw:
    fw.write("hello, funfile.")
    fw.write("another line.")

支持指定偏移量的随机写入:

with ConcurrentFile("output.bin", mode="wb") as fw:
    fw.write(b"chunk1", offset=0)
    fw.write(b"chunk2", offset=1024)

pickle 序列化

便捷的 pickle 读写封装:

pickle 可以执行序列化数据中的代码,只加载可信来源的数据。

from funfile.pickle import dump, load, dumps, loads

# 写入文件
dump({"key": "value"}, "data.pkl")

# 从文件读取
data = load("data.pkl")

# 序列化为 bytes
raw = dumps({"key": "value"})

# 从 bytes 反序列化
obj = loads(raw)

文件系统工具

from funfile.funos import makedirs, delete

# 递归创建目录(已存在不报错)
makedirs("path/to/dir")

# 删除文件或递归删除目录
delete("path/to/dir")
from funfile.file import copy

# 复制文件
copy("src.txt", "dst.txt")

获取文件/目录大小

from funfile import get_size

# 获取文件大小(字节)
size = get_size("large_file.bin")

# 递归获取目录大小
size = get_size("mydir", recursive=True)

文件哈希

from funfile import file_md5, file_sha256

md5 = file_md5("archive.tar.xz")
sha256 = file_sha256("archive.tar.xz")

项目结构

src/funfile/
├── __init__.py            # 顶层导出
├── funos.py               # 文件系统工具(makedirs, delete)
├── file/
│   ├── core.py            # 文件复制
│   └── concurrent.py      # 并发文件写入
├── compress/
│   ├── tarfile.py         # 带进度条的 tar 操作
│   ├── zipfile.py         # zip 操作封装
│   └── allfile.py         # 通用解压
├── utils/
│   ├── size.py            # 文件大小与可读格式
│   ├── hash.py            # 文件哈希
│   └── tqdm_bar.py        # 文件进度条
└── pickle/
    └── core.py            # pickle 序列化封装

许可证

Apache License 2.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

funfile-1.0.39-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file funfile-1.0.39-py3-none-any.whl.

File metadata

  • Download URL: funfile-1.0.39-py3-none-any.whl
  • Upload date:
  • Size: 18.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for funfile-1.0.39-py3-none-any.whl
Algorithm Hash digest
SHA256 1052de435bb72a89e1ac74f6ae8e1beebb866da35a062490d150c04abfeb21b7
MD5 241f51ed81c38f4923c7bb5b292d6c7a
BLAKE2b-256 67e219606f648a3bb8b993c6dcde34eb81f2f5a0ccaf722ff6e4b3730ded2ffc

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.42

1 file

This release

1.0.39 This release

1 file

1.0.27

1 file

1.0.26

1 file

1.0.25

1 file

1.0.24

2 files

1.0.17

2 files

1.0.16

2 files

1.0.15

2 files

1.0.14

2 files

1.0.13

2 files

1.0.12

2 files

1.0.11

2 files

1.0.10

2 files

1.0.8

2 files

1.0.7

2 files

1.0.6

2 files

1.0.5

2 files

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

0.2.9

1 file

0.2.8

1 file

0.2.6

1 file

0.2.5

1 file

0.2.4

1 file

0.2.3

1 file

0.2.2

1 file

0.2.1

1 file

0.2.0

1 file

0.1.31

1 file

0.1.30

1 file

0.1.29

1 file

0.1.28

1 file

0.1.27

1 file

0.1.26

1 file

0.1.25

1 file

0.1.24

1 file

0.1.23

1 file

0.1.22

1 file

0.1.21

1 file

0.1.20

1 file

0.1.19

1 file

0.1.18

1 file

0.1.17

1 file

0.1.16

1 file

0.1.15

1 file

0.1.14

1 file

0.1.13

1 file

0.1.12

1 file

0.1.11

1 file

0.1.10

1 file

0.1.9

1 file

0.1.8

1 file

0.1.7

1 file

0.1.6

1 file

0.1.5

1 file

0.1.4

1 file

0.1.3

1 file

0.1.2

1 file

0.1.1

1 file

0.1.0

1 file

0.0.31

1 file

0.0.30

1 file

0.0.29

1 file

0.0.28

1 file

0.0.27

1 file

0.0.26

1 file

0.0.25

1 file

0.0.24

1 file

0.0.23

1 file

0.0.22

1 file

0.0.21

1 file

0.0.20

1 file

0.0.19

1 file

0.0.18

1 file

0.0.17

1 file

0.0.15

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page