比较Python中的两个指针位置

时间:2015-07-09 16:41:35

标签: python python-2.7 pointers

代码

import sys
import os

fp = open("/home/masi/r3.raw", "rb")

try:
    events = []
    while aBuf[:4] != b'\xFA\xFA\xFA\xFA':
        aBuf = fp.read(4)
        events.append(aBuf)         
        if aBuf == os.SEEK_END:
            # pointer cannot be outside of file so minus 144
            fileEnding = aBuf[os.SEEK_END - 144 : os.SEEK_END]
except:
    print "File end at position : ", fp.tell()
    import traceback
    traceback.print_exc()

finally:
    fp.close()

我知道以下内容永远不会是真的

        if aBuf == os.SEEK_END:
            # pointer cannot be outside of file so minus 144
            fileEnding = aBuf[os.SEEK_END - 144 : os.SEEK_END]

我正在将指针与文件的结束指针进行比较,至少我期待这样,但它似乎不正确。

来自skrrgwasme和martineau贡献的改进代码

import sys
import os
import struct
import binascii

file_name = "/home/masi/r.raw"
file_size = os.path.getsize(file_name)
print "File size is : ", file_size
read_size = 4
read_count = 0

aBuf = b'\x00\x00\x00\x00' # don't forget to create your variables before you try to read from them
fileEnding = ""
fp = open(file_name, "rb")

try:
    aBuf = fp.read(read_size)
    read_count += read_size
    event_starts = []
    event_ends = []
    event_starts.append(read_count)
    while aBuf and read_count < file_size:
        if aBuf[:read_size] == b'\xFA\xFA\xFA\xFA':
            event_ends.append(read_count)
            if read_count + 1 < file_size: event_starts.append(read_count + 1)

        aBuf = fp.read(read_size)
        read_count += read_size
        print "RC ", read_count, ", remaining: ", 1.0-float(read_count)/file_size, "%"

    if read_count >= file_size: break

except:
    print "File end at position : ", fp.tell()
    import traceback
    traceback.print_exc()

finally:
    # store to partial index of postgres database: event pointers
    fp.close()

如何比较两个指针的位置?

1 个答案:

答案 0 :(得分:2)

如果您查看os module的Python源代码,您会发现os.SEEK_END并未自动设置为文件大小。它只是一个等于整数2 It is intended to be used as a parameter for the lseek() function的常量。

首先需要以字节为单位获取文件大小,然后将文件指针与之比较。您可以使用os.path.getsize(path)以字节为单位获取文件大小。你的比较从来都不是因为你一次读取四个字节,所以你的文件指针从字节0跳到字节4,超过2,这是os.SEEK_END的值。

建议代码:

import sys
import os

file_name = "/home/masi/r3.raw"
file_size = os.path.getsize(file_name)
read_size = 4
read_count = 0
# you could use fp.tell() in the loop instead of manually incrementing
# your own count of the file position instead, but this will avoid a lot of
# extra fp.tell() calls in the loop

aBuf = b'\x00\x00\x00\x00' # don't forget to create your variables before you try to
                           # read from them
fp = open(file_name, "rb")

try:
    events = []
    while aBuf[:read_size] != b'\xFA\xFA\xFA\xFA':
        aBuf = fp.read(read_size)
        events.append(aBuf)
        read_count += read_size         
        if read_count >= file_size:
            # pointer cannot be outside of file so minus 144
            fileEnding = aBuf[file_size - 144 : file_size]
            break
except:
    print "File end at position : ", fp.tell()
    import traceback
    traceback.print_exc()

finally:
    fp.close()

注意:

  1. 我建议使用大于或等于比较(>=),而不是将完全与您期望的文件大小进行比较。由于您一次只读取四个字节,如果您的文件大小有误,您的比较将永远不会成立。

  2. 在您使用此代码后,我建议将其移至Code Review Stack Exchange。正如martineau在评论中有所指出的那样,您的代码中存在许多值得纠正的问题和潜在缺陷。