开发者

using python to open a device for raw writing

I'm writing some code to read and interpret the MBR and then the FAT of a pen drive formatted with fat32, everything is going fine, I now want to write to the device at a specific position, so after closing the device I'd opened to read, I try again like this:

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

So i close everything up, and restart the interpreter with sudo (sudo python), and retry all with the same results.

what can I do in order to be able to open the device for write access? thanks EDIT with some more code.

开发者_StackOverflow社区
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'

in fact you could simply see the problem as this:

http://pastie.org/2521541

never got the formating to work properly...

it all seems like an privileges error, but running the interpreter with sudo yields no changes.


You should unmount the block device before opening it in Python.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜