简体   繁体   English

使用kernel32.dll读取文件(Python)

[英]Read file with kernel32.dll (Python)

I need to read file using kernel32.dll. 我需要使用kernel32.dll读取文件。 I have read about CreateFile, SetFilePointer and ReadFile methods (I need to use them) - but not sure how to apply them. 我已经阅读了有关CreateFile,SetFilePointer和ReadFile方法的信息(我需要使用它们)-但不确定如何应用它们。

As mentioned at msdn I need to specify all those arguments. msdn所述我需要指定所有这些参数。 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. 任何人都可以通过kernel32.dll CreateFile,SetFilePointer和ReadFile上的“ howto”帮助我。

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. 我尝试使用kernel32的原因是,我需要一种真正非常快的方式来读取闪存驱动器。 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. 该代码大约需要8分钟才能通过USB浏览1 Gb文件。

I need it to be done at 4 minutes. 我需要在4分钟时完成。

If someone has any suggestions how I can do it other way - I would really appreciate. 如果有人对我可以采取其他方法提出建议,我将不胜感激。

Update 更新资料

I have tried win32file (pywin32) like this: 我已经尝试过像这样的win32file(pywin32):

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. 比python的“ seek”和“ read”函数花费更多的时间(12分钟)。 Does anyone know why usage of module for Windows dll increases time of reading? 有谁知道为什么使用Windows dll模块会增加读取时间?

Here's the most common use of CreateFile : 这是CreateFile的最常见用法:

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

And ReadFile : 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 . 请注意,如果使用ctypes ,则对于pOutput (将在其中存储文件内容),将需要c_char_p (代表字符串),对于lpNumberOfBytesRead将需要byrefpointer

You can also use pywin32 which I'm sure will be simpler. 您也可以使用pywin32 ,我相信它会更简单。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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