2011-09-12 15 views
1

我正在寫一些代碼來讀取和解釋MBR,然後用FAT32格式化的筆式驅動器的FAT,一切都很順利,我現在想寫在設備上一個特定的位置,所以最後,我想打開讀取設備後,我再試試這樣:使用python打開設備的原始文字

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

所以我關閉了一切,並重新啓動使用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運行解釋器不會產生任何變化。

+0

你可以修剪你的代碼到絕對(自足的)最小值來演示問題,並將結果添加到你的問題? – NPE

回答

1

在Python中打開它之前,您應該卸載塊設備。

+0

所以爲了讀取它的好,如果它被安裝? – cromestant

+0

好吧,卸載塊設備是答案(不是/ dev/disk5本身,但分區'/ dev/disk5s1')現在,如果只有我可以從python做到這一點。 – cromestant