I am trying to read a file that contains an ASCII header and binary data sections, but the Python interpreter appears to close the file prematurely (i.e. before the end of file is reached). Here is my code, developed in 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
However, the file is closed prematurely after around 1MB of the total 77MB of the file has been read.
print 'File size:',fileSize
print 'Total read:',bytesRead
produces: File size: 76658457, Total read: 1165436
It exits within one of the binary sections. I moified the original program to iteratively re-open the file from the point that it was closed, as follows:
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
This gave:
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
Any idea how I can persuade Python not to keep closing the file, and just read it all in one go?