You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.0 KiB
62 lines
2.0 KiB
11 months ago
|
# -*- 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
|
||
|
import pandas as pd
|
||
|
|
||
|
columns = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
|
||
|
indexs = []
|
||
|
|
||
|
my_dict = {i: [] for i in columns}
|
||
|
|
||
|
# 定义常量
|
||
|
SECTOR_SIZE = 512 # 扇区大小
|
||
|
BYTES_PER_LINE = 16 # 每行字节数
|
||
|
MAX_SECTORS_TO_READ = 15927 # 要读取的最大扇区数量
|
||
|
# MAX_SECTORS_TO_READ = 10
|
||
|
# 打开设备文件
|
||
|
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)
|
||
|
|
||
|
# 输出扇区数据
|
||
|
for i in range(0, len(sector_data), BYTES_PER_LINE):
|
||
|
line_data = sector_data[i:i + BYTES_PER_LINE]
|
||
|
indexs.append(f"{byte_offset + i:08X}:")
|
||
|
n = 0
|
||
|
for byte in line_data:
|
||
|
my_dict[columns[n]].append(f"{byte:02X}")
|
||
|
n += 1
|
||
|
|
||
|
# 更新扇区号,读取下一个扇区
|
||
|
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}")
|
||
|
|
||
|
df = pd.DataFrame(my_dict, index=indexs)
|
||
|
print(df)
|
||
|
df.to_csv("output.csv")
|
||
|
print('输出完毕')
|