1try: 2 import uerrno 3 import uos 4except ImportError: 5 print("SKIP") 6 raise SystemExit 7 8try: 9 uos.VfsFat 10except AttributeError: 11 print("SKIP") 12 raise SystemExit 13 14 15class RAMFS_OLD: 16 17 SEC_SIZE = 512 18 19 def __init__(self, blocks): 20 self.data = bytearray(blocks * self.SEC_SIZE) 21 22 def readblocks(self, n, buf): 23 # print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf))) 24 for i in range(len(buf)): 25 buf[i] = self.data[n * self.SEC_SIZE + i] 26 27 def writeblocks(self, n, buf): 28 # print("writeblocks(%s, %x)" % (n, id(buf))) 29 for i in range(len(buf)): 30 self.data[n * self.SEC_SIZE + i] = buf[i] 31 32 def sync(self): 33 pass 34 35 def count(self): 36 return len(self.data) // self.SEC_SIZE 37 38 39try: 40 bdev = RAMFS_OLD(50) 41except MemoryError: 42 print("SKIP") 43 raise SystemExit 44 45uos.VfsFat.mkfs(bdev) 46vfs = uos.VfsFat(bdev) 47uos.mount(vfs, "/ramdisk") 48 49# file io 50with vfs.open("file.txt", "w") as f: 51 f.write("hello!") 52 53print(list(vfs.ilistdir())) 54 55with vfs.open("file.txt", "r") as f: 56 print(f.read()) 57 58vfs.remove("file.txt") 59print(list(vfs.ilistdir())) 60