使用标准python文件api写入索引文件序列
filesequence的Python项目详细描述
文件序列
FileSequence允许您使用标准 python文件描述符读/写功能。
在打开 序列,但库根据需要处理创建新文件。
每次调用打开的filesequence的write函数时 如果要写入的块将推送 文件超过了限制。所以如果你只想在换行符上拆分文件, 每行调用write()一次。如果你想表现得更像 bsd的split命令,一次只能写入一个字节。不过,在 这个价格,split可能是更好的选择。
api
您可以像使用文件一样简单地使用filesequence对象。
- filesequence.open(...)返回一个FileSequence对象。
- my_file_sequence.write(line)取一行并将其写入 下一个可用文件。
注意,文件序列需要with包装,而不是 python内置open():
import filesequence filenames = filesequence.interpolator('numbers-%02d.txt', xrange(1000)) with filesequence.open(filenames, 1000000) as out: for a in xrange(1000): for b in xrange(1000): out.write('# %d * %d = %d\n' % (a, b, a * b))
现在在20个不同的文件中有一个巨大的乘法表 1MB或更低!太棒了!
想继续吗?
filenames = filesequence.interpolator('numbers-%02d.txt', xrange(1000)) with filesequence.open(filenames, 1000000, 'a') as out: ...
“a”标志将使序列跳转到最后一个现有文件, 从那里开始写作。
奖金
一个filesequence脚本将安装到您的PATH中。这个 脚本逐行读取 文件名pattern和文件大小限制(请参见filesequence --help), 写出一系列文件,文件大小不超过 打破任何界限。
$ filesequence --help usage: cli.py [-h] [--limit LIMIT] [--pattern PATTERN] [--version] Write STDIN into a sequence of files, splitting only at newlines optional arguments: -h, --help show this help message and exit --limit LIMIT Maximum bytes per file (default: 50000000) --pattern PATTERN Filename string pattern: generate filenames in sequence by interpolating `pattern % indices.next()` (default: split.%02d) --version show program's version number and exit
待办事项
- 支持读取(标志r和r+)。
开发
此包发布到pypi pypi.python.org/pypi/filesequence。
典型的发布过程:
- pandoc README.md -o README.rst
- 如果需要,git commit ...
- npm version patch
- git push
- python setup.py register sdist upload
许可证
版权所有(c)2013克里斯托弗·布朗。MIT Licensed。