使用python数组fromfile读取数据时出错

时间:2015-11-01 23:08:18

标签: python binaryfiles pcap

我正在尝试在Python 2.7中读取二进制pcap文件。

import array
f = open('unit_46_Monterey_subset.pcap')
bin = array.array('B')
bin.fromfile(f, 1206)

文件大小为12.640.024字节。

当我运行它时,我收到错误消息:EOFError:'文件中没有足够的项目'

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的opening文件处于错误的模式:而不是'r'(默认值),您需要打开'rb',用于读取二进制文件。这应该有效:

import array
f = open('unit_46_Monterey_subset.pcap', 'rb')  # notice the 2nd argument
bin = array.array('B')
bin.fromfile(f, 1206)

当然,完成后不要忘记close文件:

f.close()