Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - Read file using win32file.ReadFile

Similar Question:

What's the correct way to use win32file.ReadFile to get the output from a pipe?

The issue I have however was not answered in that question. When I call

result, data = win32file.ReadFile(my_file, 4096, None)

result is always 0 which according to documentation means success:

The result is a tuple of (hr, string/PyOVERLAPPEDReadBuffer), where hr may be 0, 
ERROR_MORE_DATA or ERROR_IO_PENDING.

Even if I set the buffer to 10 and the file is much bigger the result is 0 and data is a string containing the first 10 characters.

result, buf = win32file.ReadFile(self._handle, 10, None)  
while result == winerror.ERROR_MORE_DATA:            
    result, data = win32file.ReadFile(self._handle, 2048, None)
    buf += data   
    print "Hi"
return result, buf

"Hi" is never printed even if the file clearly contains more data. The problem I have is how can I ensure that I'm reading the whole file without using a ridiculous large buffer?

like image 364
beginner_ Avatar asked Nov 01 '22 05:11

beginner_


1 Answers

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.

like image 128
beginner_ Avatar answered Nov 15 '22 05:11

beginner_