python - Read file using win32file.ReadFile
As was already observed is that if the win32file.ReadFile
result value hr
is 0, then that means success. This is exactly the opposite from the win32 api documentation that says 0 means an error occurred.
To determine how many bytes were read you need to check the length of the returned string. If it is same size as the buffer size, then there might be more data. If it is smaller, the whole file has been read:
def readLines(self):
bufSize = 4096
win32file.SetFilePointer(self._handle, 0, win32file.FILE_BEGIN)
result, data = win32file.ReadFile(self._handle, bufSize, None)
buf = data
while len(data) == bufSize:
result, data = win32file.ReadFile(self._handle, bufSize, None)
buf += data
return buf.split('\r\n')
You need to add error handling to this, eg. check result if it actually is 0 and if not take according measures.