脚本在文件结束前终止读取

时间:2016-12-02 17:20:11

标签: python binary ascii

我正在尝试读取包含ASCII标头和二进制数据部分的文件,但Python解释器似乎过早地关闭了文件(即在文件结束之前)。这是我在Python 2.7.12中开发的代码:

fileSize = os.path.getsize(filename) # file size in bytes
bytesRead = 0L
content = []
with open(filename,'r') as f:
    content = f.read()
    bytesRead += sys.getsizeof(content)

print 'File size:',fileSize
print 'Total read:',bytesRead

但是,在读取了总共77MB的文件大约1MB后,文件会过早关闭。

print 'File size:',fileSize
print 'Total read:',bytesRead

生成:File size: 76658457, Total read: 1165436

它在一个二进制部分中退出。我将原始程序模仿,以便从关闭时重复打开文件,如下所示:

fileSize = os.path.getsize(filename) # file size in bytes
bytesRead = 0L
content = []

try:
    while True:
        count += 1
        with open(filename,'r') as f:
            f.seek(bytesRead+1)
            newContent = f.read()
            content.append(newContent)
            bytesRead += sys.getsizeof(newContent)
            print count,' Total read:',bytesRead
except Exception,e:
    print e

print 'File size:',fileSize
print '% read = ',bytesRead*100./float(fileSize)
print 'count: ',count

这给了:

1 Total read: 1165436
2 Total read: 1180218
3 Total read: 1181902

... [many more iterations] ...

25564 Total read: 77925641
25567 Total read: 77926615
25568 Total read:Exception: I/O operation on closed file

File size: 76658457
% read =  101.65429721603
count:  25568

任何想法如何说服Python不要继续关闭文件,只需一次阅读就可以了?

1 个答案:

答案 0 :(得分:0)

我认为发生的是你正在检查字节大小,而不是数组中的实际数据。检查数组的最后一行,然后检查文件的最后一行,它很可能会揭示它实际上正在读取整个文件的事实