用于逐字节读取文件的迭代器

时间:2016-02-14 02:52:11

标签: python python-2.7 file iterator byte

是否有用于读取文件byte by byte的迭代器?

2 个答案:

答案 0 :(得分:2)

没有逐字节的迭代器,但创建生成器更容易:

def bytefile(f):
    b = f.read(1)
    while b:
        yield b
        b = f.read(1)

with open('<file>', 'rb') as f:
    for b in bytefile(f):
        <do something>

但这并不是非常有效,并且不清楚你想要做什么。

答案 1 :(得分:0)

如果您使用的是* nix或Windows,则可以mmap该文件。这应该是迭代文件字节的有效方法:

{
    "name": "vue-test",
    "version": "1.0.0",
    "dependencies": {
        "vue": "^1.0.16"
    },
    "devDependencies": {
        "babel-core": "^6.1.2",
        "babel-loader": "^6.1.0",
        "babel-plugin-transform-runtime": "^6.1.2",
        "babel-preset-es2015": "^6.1.2",
        "babel-preset-stage-0": "^6.1.2",
        "babel-runtime": "^5.8.0",
        "webpack": "^1.12.2",
        "css-loader": "^0.23.0",
        "style-loader": "^0.13.0",
        "vue-loader": "^7.3.0",
        "vue-html-loader": "^1.0.0",
        "vue-hot-reload-api": "^1.2.0"
    }
}