Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python read(n)函数返回的字节数少于n个,且未达到EOF_Python_Python 2.7_Parsing - Fatal编程技术网

Python read(n)函数返回的字节数少于n个,且未达到EOF

Python read(n)函数返回的字节数少于n个,且未达到EOF,python,python-2.7,parsing,Python,Python 2.7,Parsing,我试图解析一个文件,但遇到了以下问题:在从文件读取DWORD时,我遇到了一个点,它只读取1字节而不是4字节,并且EOF是而不是命中 address_of_functions = [] DWORD = "<I" size_dword = struct.calcsize(DWORD) f.seek(rva2fa(export_directory.AddressOfFunctions, section_header,nt_header.FileHeader.NumberOfSection

我试图解析一个文件,但遇到了以下问题:在从文件读取DWORD时,我遇到了一个点,它只读取1字节而不是4字节,并且EOF是而不是命中

address_of_functions = []    
DWORD = "<I"
size_dword = struct.calcsize(DWORD)
f.seek(rva2fa(export_directory.AddressOfFunctions, section_header,nt_header.FileHeader.NumberOfSections), 0)
    for i in range(export_directory.NumberOfFunctions/2):
        buffer = f.read(size_dword)
        buffer1 = f.read(size_dword)
        print i, len(buffer)
        print i, len(buffer1)
        address_of_functions.append(struct.unpack(DWORD, buffer)) 
函数的地址=[]
DWORD=“如果文件是二进制文件——也就是说,它可能包含具有任何值的字节,而不仅仅是可打印字符——您必须以二进制模式“rb”打开它

在Windows上尤其如此,因为如果以文本模式打开文件,如果底层系统库读取函数看到值为26(十六进制0x1A)的字节(对应于Ctrl-Z),它将报告EOF


以二进制模式打开文件可以避免这一点和其他不便。

您没有显示打开文件的代码-您打开了吗”rb“?文件末尾是否有不需要的EOL
\n
?您的缩进似乎不稳定,Python不喜欢不稳定的缩进。
NumberOfFunctions
的值是否与文件内容匹配(即存在的函数数的两倍)?@Tandura:是的,我看到了。但是查看
buffer
buffer1
中的实际字符可能会有所启发。更好的方法是,使用
rb
打开,这将导致
read
返回
bytes
对象,并查看十六进制的内容。