2011-07-27 27 views
1

我比預期的寫入文件的麻煩多得多。我有一臺運行在Angstrom嵌入式發行版的ARM處理器上的小型單板計算機。我正在用python編寫一個應用程序。應用程序應該檢測USB棒,然後將文件轉儲到它。我遇到的問題是,即使當我的腳本似乎正確寫入文件,並且該文件似乎在Linux中使用「ls」和「cat」時,當我移除USB存儲棒並嘗試查看在Windows或其他Linux發行版中的文件是空的或更多的時候根本不存在。我的應用程序是多線程的。我創建/ media/mymntpnt作爲根(目錄)。在Python中用Python將文件寫入USB存儲棒?

任何幫助表示讚賞。

下面是我的一些代碼片段:

這部分是我用來識別USB棒:

class MediaScanner(): 
    def __init__(self): 
     self.lok = thread.allocate_lock() 
     self.running = True 
     self.started = False 

    def register_cb(self,func): 
     self.cb = func 

    def start(self): 
     if not self.started: 
      thread.start_new_thread(self.scan_thread,()) 

    def scan_thread(self): 
     self.quit = False 
     self.started = True 
     last_devices = [] 
     while self.running: 
      devices = self.scan_media() 
      if (devices != last_devices): 
       self.cb(devices) #call the callback as its own thread 
      last_devices = devices 

      time.sleep(0.1) 

     self.quit = True  

    def stop(self): 
     self.running = False 
     while(not self.quit): 
      pass 
     return True 

    def is_running(self): 
     return self.running 


    def scan_media(self): 
     with self.lok: 
      partitionsFile = open("/proc/partitions") 
      lines = partitionsFile.readlines()[2:]#Skips the header lines 
      devices = [] 
      for line in lines: 
       words = [x.strip() for x in line.split()] 
       minorNumber = int(words[1]) 
       deviceName = words[3] 
       if minorNumber % 16 == 0: 
        path = "/sys/class/block/" + deviceName 
        if os.path.islink(path): 
         if os.path.realpath(path).find("/usb") > 0: 
          devices.append('/dev/'+deviceName) 

      partitionsFile.close() 

      return devices 

這是我的腳本示例寫入文件:

from mediascanner import * 
from util import * 
from database import * 
from log import * 

ms = MediaScanner() 

devices = ms.scan_media() 

print devices 

if devices: 
    db = TestDatabase(init_tables=False) 

    data = db.get_all_test_data() 



    for device in devices: 
     print device 
     mount_partition(get_partition(device)) 
     write_and_verify('/media/mymntpnt/test_'+get_log_file_name(),flatten_tests(data)) 
     unmount_partition() 

這裏是我的實用功能:

def write_and_verify(f_n,data): 
    f = file(f_n,'w') 
    f.write(data) 
    f.flush() 
    f.close() 
    f = file(f_n,'r') 
    verified = f.read() 
    f.close() 
    return verified == data and f.closed 


def get_partition(dev): 
    os.system('fdisk -l %s > output' % dev) 
    f = file('output') 
    data = f.read() 
    print data 
    f.close() 
    return data.split('\n')[-2].split()[0].strip() 

def mount_partition(partition): 
    os.system('mount %s /media/mymntpnt' % partition) 


def unmount_partition(): 
    os.system('umount /media/mymntpnt') 
+0

你嘗試過'sync'和/或'pmount'和'pumount'嗎?那種只是頭腦風暴。 – unmounted

+0

也可以看看'os.fdatasync'(可能相當於linux上的'os.system('fsync ...)') – unmounted

+0

所以有時候你看到計算機上的文件? – cwoebker

回答

0

錯誤在於寫入函數,它應該在關閉它之前同步文件,如下所示。讀取將始終成功,因爲數據已經在RAM中。您可能想嘗試一些不同的方法來驗證它,例如檢查ByteCount。

def write_and_verify(f_n,data): 
    f = file(f_n,'w') 
    f.write(data) 
    f.flush() 
    os.fsync(f.fileno()) 
    f.close() 
    f = file(f_n,'r') 
    verified = f.read() 
    f.close() 
    return verified == data and f.closed 

如果你喜歡這個finfo = os.stat(f_n)文件信息,那麼你可以與你預期的要寫入的字節數比較finfo.st_size