使用python打开设备进行原始编写

时间:2011-09-12 14:28:47

标签: python macos device

我正在编写一些代码来读取和解释MBR,然后是用fat32格式化的笔式驱动器的FAT,一切都很顺利,我现在想要在特定位置写入设备,所以关闭设备后我打开阅读,我再试一次:

dr = file("/dev/disk5","r+")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
 IOError: [Errno 16] Resource busy: '/dev/disk5'

所以我关闭所有内容,然后使用sudo(sudo python)重新启动解释器,并以相同的结果重试所有内容。

为了能够打开设备进行写访问,我该怎么办? 谢谢 使用更多代码编辑。

import sys,os
disk = file("/dev/disk5",'rb')
disk.seek(0)
sector_size=512
first_sector = disk.read(1*sector_size)
fat_part_list = (first_sector[-66:])[:64]
part1=fat_part_list[:16]
#more code here in order to analyse the first partition information in the MBR and get the required offset to actually read the partition itself.
#now for instance lets say I want to write here in sector 1 byte 0 ( so basically at seek(0).
disk.close()#close the device
disk = file("/dev/disk5","r+")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  IOError: [Errno 16] Resource busy: '/dev/disk5'
事实上你可以简单地看到这个问题:

http://pastie.org/2521541

从未让格式化正常工作......

这一切似乎都是一个特权错误,但是使用sudo运行解释器不会产生任何变化。

1 个答案:

答案 0 :(得分:1)

在使用Python打开块设备之前,应先将其卸载。