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

3 投票
1 回答
1192 浏览
提问于 2025-04-15 22:09

我试着运行以下代码,来自这个链接: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 个回答

8

我觉得你把模块命名为 mmap.py 有点奇怪,这样会让导入的时候搞混,结果导入了同一个文件... 试着把名字换成别的(最好不要用标准库里已有的模块名 :p)

撰写回答