Skip to content Skip to sidebar Skip to footer

Writing A File To A Usb Stick In Linux With Python?

I'm having a lot more trouble than expected writing to a file than I expected. I have a small Single Board computer running on an arm processor with the Angstrom embedded distribut

Solution 1:

The mistake is in the write function which should sync the file, before closing it, as seen below. The read will always succeed because the data is already in RAM. You might want to try something different to verify it such as checking the 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

If you get the file info like this finfo = os.stat(f_n) then you can compare finfo.st_size with the number of bytes that you expected to write.

Post a Comment for "Writing A File To A Usb Stick In Linux With Python?"