提供类似maildir的文件访问
项目描述
Filestore
filestore是一种在无需加锁的情况下处理多个进程中的文件的方法。
初始化FileStore
在临时区域创建filestore
>>> import tempfile, os >>> temp_dir = tempfile.mkdtemp() >>> store_dir = os.path.join(temp_dir, 'store1') >>> os.mkdir(store_dir) >>> from gocept.filestore import FileStore >>> filestore = FileStore(store_dir) >>> filestore <gocept.filestore.filestore.FileStore object at 0x...>
到目前为止,没有发生任何事情
>>> import os >>> os.listdir(store_dir) []
在使用存储之前,我们需要准备它
>>> filestore.prepare()
准备已创建tmp/new/cur目录结构
>>> sorted(os.listdir(store_dir)) ['cur', 'new', 'tmp']
再次调用准备没有作用
>>> filestore.prepare() >>> sorted(os.listdir(store_dir)) ['cur', 'new', 'tmp']
如果删除了 store_dir,则会通过调用 prepare 重新创建。
>>> import shutil >>> shutil.rmtree(store_dir) >>> os.listdir(temp_dir) [] >>> filestore.prepare() >>> os.listdir(temp_dir) ['store1'] >>> sorted(os.listdir(store_dir)) ['cur', 'new', 'tmp']
使用FileStore
添加文件到存储可以使用 create 方法
>>> f = filestore.create('a-file')
文件以 'w' 模式(如果未指定)创建在 'tmp' 区域
>>> f.name '.../tmp/a-file' >>> f.mode 'w'
我们在 tmp 区域找到了文件。注意,filestore.list 列出了带有完整路径名称的文件,因此我们可以直接将名称传递给 file/open
>>> filestore.list('tmp') ['.../tmp/a-file']
我们得到了一个普通文件,因此可以写入它
>>> _ = f.write('Die Ente bleibt draussen!') >>> f.close()
我们已完成文件的写入,因此可以将其移动到 new 空间以便其他应用程序拾取
>>> filestore.move('a-file', 'tmp', 'new') >>> filestore.list('tmp') [] >>> filestore.list('new') ['.../new/a-file']
移动操作使用 os.move,它应该是原子的。当其他进程“看到”文件时,它可以直接使用它并将其移动到 'cur' 目录
>>> filestore.move('a-file', 'new', 'cur') >>> filestore.list('new') [] >>> filestore.list('cur') ['.../cur/a-file']
文件也可以复制
>>> filestore.copy('a-file', 'cur', 'tmp') >>> filestore.list('cur') ['.../cur/a-file'] >>> filestore.list('tmp') ['.../tmp/a-file']
最后,可以删除文件
>>> filestore.remove('a-file', 'cur') >>> filestore.list('cur') []
清理
测试后删除临时目录
>>> import shutil >>> shutil.rmtree(store_dir)
变更
1.0 (2023-07-14)
取消对 Python 2.7、3.5 和 3.6 的支持。
0.5 (2023-03-16)
添加对 Python 3.9、3.10 和 3.11 的支持。
使用 GitHub actions 作为 CI。
0.4 (2019-11-29)
将仓库迁移到 Bitbucket。
将仓库迁移到 GitHub。
已使 Python 3 兼容(已与 Python 2.7、3.7 和 3.8 进行测试)。
用 tox 替换 bootstrap/buildout。
将测试覆盖率提高到 100%。
0.3 (2009-10-08)
添加了 copy() 方法。
0.2 (2007-08-30)
首次公开发布。
项目详情
关闭
gocept.filestore-1.0.tar.gz 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 3fbec68b20cf2b2df54d046aafb6a382a83a772e1b5c59b459c44ec4dd843786 |
|
MD5 | e3016febe64d187f6c1719a35fe3dac3 |
|
BLAKE2b-256 | e5a798c1abc3c87a7a7a3aade652b7fea069c6a49429f14e087451bdcf0e7f52 |
关闭
gocept.filestore-1.0-py2.py3-none-any.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | f2488366f26e8c32e7c53fcb09e8d22c1918c4bcbbeba6a9afa1324c471bbcc6 |
|
MD5 | 6dcd48c8a251f57ee3055d20cd145141 |
|
BLAKE2b-256 | 8f25069ec6906b96827a4ccb9ed6af458964d7320c8e331a5d5de2da57a4451b |