予想以上にファイルへの書き込みに問題があります。私はAngstrom組み込みディストリビューションのアーム・プロセッサー上で動作する小さなSingle Boardコンピューターを持っています。私はPythonでそれのためのアプリケーションを書いています。アプリケーションは、USBスティックを検出し、そのファイルをUSBスティックにダンプする必要があります。私が実行している問題は、スクリプトがファイルを正しく書き込むように見えていて、ファイルがLinuxで "ls"と "cat"を使っているように見える場合でも、USBスティックを外してWindowsや他のLinuxディストリビューションのファイルは空であるか、まれに存在しないことがあります。私のアプリケーションはマルチスレッド化されています。/media/mymntpntをルート(ディレクトリ)として作成します。Pythonでlinuxの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
そしてここでは、ファイルの書き込みに私のスクリプトの例です
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')
:
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()
そして、ここでは私のユーティリティ関数です
あなたは 'sync'や' pmount'と 'pumount'を試しましたか?ちょうどブレーンストーミングの種類。 – unmounted
'os.fdatasync'を見てください(linuxでは' os.system( 'fsync ...) 'のようなものでしょう) – unmounted
あなたのコンピュータ上のファイルは時々見えますか? – cwoebker