在python 2.6中运行mmap相关函数时出错

时间:2010-04-29 14:55:51

标签: python mmap

我尝试从http://docs.python.org/library/mmap.html

运行以下代码
import mmap

# write a simple example file
with open("hello.txt", "wb") as f:
    f.write("Hello Python!\n")

with open("hello.txt", "r+b") as f:
    # memory-map the file, size 0 means whole file
    map = mmap.mmap(f.fileno(), 0)
    # read content via standard file methods
    print map.readline()  # prints "Hello Python!"
    # read content via slice notation
    print map[:5]  # prints "Hello"
    # update content using slice notation;
    # note that new content must have same size
    map[6:] = " world!\n"
    # ... and read again using standard file methods
    map.seek(0)
    print map.readline()  # prints "Hello  world!"
    # close the map
    map.close()

但是,我收到了一个错误。

TypeError: 'module' object is not callable

module body in mmap.py at line 9
map = mmap.mmap(f.fileno(), 0)

这有什么问题? 我在Snow Leopard / Mac上使用python 2.6。

1 个答案:

答案 0 :(得分:4)

我认为你正在做一些奇怪的事情,调用你的模块mmap.py,导入变得混乱并导入相同的文件而不是...尝试将名称改为其他东西(最好不是标准的库模块名称) :p)的