简体   繁体   中英

Read file with kernel32.dll (Python)

I need to read file using kernel32.dll. I have read about CreateFile, SetFilePointer and ReadFile methods (I need to use them) - but not sure how to apply them.

As mentioned at msdn I need to specify all those arguments. I know that first is path to file, but have no idea what are those other arguments and in what way should I insert them.

Can anyone help me with "howto" on kernel32.dll CreateFile, SetFilePointer and ReadFile.

Really appreciate any help.

Update

The reason I try to use kernel32 is that I need really-really fast way to read from flash-drive. I need some way that will read directly from flash, without spending time in its file system. By this I mean direct access to required byte, without some long searches in file system.

Here is code I used for reading

    index = 524288
    data = open('Path-to-file', 'rb')
    while index < file_length:
        data.seek(index)
        headers.append((index, data.read(128)))
        index += cluster
    data.close()

This code takes about 8 minutes to look through 1 Gb file via USB.

I need it to be done at 4 minutes.

If someone has any suggestions how I can do it other way - I would really appreciate.

Update

I have tried win32file (pywin32) like this:

def get_headers(self):
    while self.index < self.file_len:
        handle = win32file.CreateFile(self.path, win32file.GENERIC_READ,
                                      win32file.FILE_SHARE_READ, None,
                                      win32file.OPEN_EXISTING,
                                      win32file.FILE_ATTRIBUTE_NORMAL, None)
        win32file.SetFilePointer(handle, self.index, win32file.FILE_CURRENT)
        header_ = win32file.ReadFile(handle, 128, None)
        header = header_[1]
        self.headers.append((self.index, header))
        self.index += cluster
        win32file.CloseHandle(handle)

It takes more time (12 minutes) than python "seek" and "read" functions. Does anyone know why usage of module for Windows dll increases time of reading?

Here's the most common use of CreateFile :

hFile = CreateFile("file.txt", GENERIC_READ, 0, 0, 3, 128, None)

And ReadFile :

ReadFile(hFile, pOutput, nNumberOfBytesToRead, lpNumberOfBytesRead, 0)

Note that, if you use ctypes , you'll need c_char_p (which represents a string) for pOutput (where the file content will be stored), and byref or pointer for lpNumberOfBytesRead .

You can also use pywin32 which I'm sure will be simpler.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM