在python中从磁盘设备读取时无法检测文件结尾

在python中从磁盘设备读取时无法检测文件结尾,python,windows,binary,device,eof,Python,Windows,Binary,Device,Eof,我在Windows8上使用的是python 3.3 32位。我想从物理磁盘读取二进制扇区。我可以从设备上打开、查找、读取和告知,但文件结尾不会产生空的读取结果,它会抛出权限异常(33)。我也无法使用相对于端点的seek,例如seek(-512,os.seek\u end)。任何使用SEEK_END或2都会抛出无效参数 我真的不希望使用权限异常来检测eof,因为当读取设备时,可能会出现真正的权限错误,并且需要警告用户此故障 我欢迎任何关于这里出了什么问题的提示,或者其他检测eof的方法 代码示例如

我在Windows8上使用的是python 3.3 32位。我想从物理磁盘读取二进制扇区。我可以从设备上打开、查找、读取和告知,但文件结尾不会产生空的读取结果,它会抛出权限异常(33)。我也无法使用相对于端点的seek,例如seek(-512,os.seek\u end)。任何使用SEEK_END或2都会抛出无效参数

我真的不希望使用权限异常来检测eof,因为当读取设备时,可能会出现真正的权限错误,并且需要警告用户此故障

我欢迎任何关于这里出了什么问题的提示,或者其他检测eof的方法

代码示例如下,该设备是1GBit USB。seek/tell/prints显示eof前读数正常

代码:

输出:

1021047296 1021047808 1021048320 1021048832 1021049344 1021049856 1021050368回溯(最近一次调用上次):文件 “D:\Development\eclipse\test\test.py”,第25行,在 扇区=磁盘。读取(512)IOError:[Errno 13]权限被拒绝


如果无法读取驱动器(或文件)末尾的整个数据块,则权限被拒绝。这是一个例子。另外,请确保以管理员身份运行Python。(使用Windows 10和Python 3.8.2)


这是文本处理脚本工具的一个新的使用领域。我不能说我同意,但至少它不是PHP。您对如何输出磁盘映像/结果有何评论,以及为什么?
device = r'\\.\PhysicalDrive2'  
disk = open(device,'rb')  
disk.seek(1994231*512)  
sector = disk.read(512)  
while sector!="":  
    sector = disk.read(512)  
    print(disk.tell()) 
import re
import time

starttime = time.time()

stuff = []
chunk = 512
with open(r"\\.\physicaldrive11",'rb') as file_obj: #Physical Drive
#with open("test.bin",'rb') as file_obj: #File
    while True:
        try:
            data=file_obj.read(chunk)
            #do cool stuff and append to list
            #stuff.append(cool stuff)
            #print (stuff)
            print (data) # if you want to see what it is doing... use a small drive, unless you want to wait forever  :-)
            
            if data == b'':
                file_obj.close()
                break

        except PermissionError:
            chunk = chunk-1
            print ("Trying smaller chunk",chunk)
            continue

endtime = time.time()
timerun = endtime - starttime
print (timerun)