|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
@File : disk_read.py
|
|
|
|
|
@License : (C)Copyright 2021-2023
|
|
|
|
|
|
|
|
|
|
@Modify Time @Author @Version @Description
|
|
|
|
|
------------ ------- -------- -----------
|
|
|
|
|
2023/10/9 10:04 zart20 1.0 None
|
|
|
|
|
"""
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
# 定义常量
|
|
|
|
|
SECTOR_SIZE = 512 # 扇区大小
|
|
|
|
|
BYTES_PER_LINE = 16 # 每行字节数
|
|
|
|
|
# MAX_SECTORS_TO_READ = 15927 # 要读取的最大扇区数量
|
|
|
|
|
MAX_SECTORS_TO_READ = 0
|
|
|
|
|
# 打开设备文件
|
|
|
|
|
drive_letter = "I:" # 请替换为你要操作的磁盘
|
|
|
|
|
file_path = f"\\\\.\\{drive_letter}" # 设备文件路径
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
with open(file_path, 'rb') as disk:
|
|
|
|
|
sector_number = 0 # 起始扇区号
|
|
|
|
|
byte_offset = 0 # 字节偏移量初始化
|
|
|
|
|
sectors_read = 0 # 已读取的扇区数量
|
|
|
|
|
|
|
|
|
|
while sectors_read <= MAX_SECTORS_TO_READ:
|
|
|
|
|
# 读取扇区数据
|
|
|
|
|
disk.seek(sector_number * SECTOR_SIZE)
|
|
|
|
|
sector_data = disk.read(SECTOR_SIZE)
|
|
|
|
|
|
|
|
|
|
# 输出扇区数据
|
|
|
|
|
print(f"扇区号:{sector_number}")
|
|
|
|
|
for i in range(0, len(sector_data), BYTES_PER_LINE):
|
|
|
|
|
line_data = sector_data[i:i + BYTES_PER_LINE]
|
|
|
|
|
print(f"{byte_offset + i:08X}:", end=" ") # 输出字节偏移量(16进制)
|
|
|
|
|
for byte in line_data:
|
|
|
|
|
print(f"{byte:02X}", end=" ") # 输出每个字节的16进制表示
|
|
|
|
|
|
|
|
|
|
print() # 换行
|
|
|
|
|
|
|
|
|
|
# 更新扇区号,读取下一个扇区
|
|
|
|
|
sector_number += 1
|
|
|
|
|
byte_offset += len(sector_data) # 累加字节偏移量
|
|
|
|
|
sectors_read += 1
|
|
|
|
|
|
|
|
|
|
except PermissionError:
|
|
|
|
|
print("没有足够的权限来访问磁盘。请以管理员身份运行程序。")
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
print(f"找不到设备文件:{file_path}")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"发生错误:{e}")
|