It is useful to store data in a persistent manner so that it remains intact between restarts of the device. On traditional computers this is often achieved by a file system consisting of named files that hold raw data, and named directories that contain files. Python supports the various operations needed to work with such file systems.
以持久方式存储数据非常有用,以便在设备重启之间保持完整。在传统计算机上,这通常是通过文件系统实现的,该文件系统由保存原始数据的命名文件和包含文件的命名目录组成。Python 支持使用此类文件系统所需的各种作。
However, since the micro:bit is a limited device in terms of both hardware and storage capacity MicroPython provides a small subset of the functions needed to persist data on the device. Because of memory constraints there is approximately 30k of storage available on the file system.
但是,由于 micro:bit 在硬件和存储容量方面都是一个有限的设备,MicroPython 提供了在设备上持久保存数据所需的一小部分功能。由于内存限制,文件系统上大约有 30k 的可用存储空间 。
Warning 警告
Re-flashing the device will DESTROY YOUR DATA.
重新刷写设备将销毁您的数据。
Since the file system is stored in the micro:bit’s flash memory and flashing the device rewrites all the available flash memory then all your data will be lost if you flash your device.
由于文件系统存储在 micro:bit 的闪存中,并且刷写设备会重写所有可用的闪存,因此如果你刷写设备,你的所有数据都将丢失。
However, if you switch your device off the data will remain intact until you either delete it (see below) or re-flash the device.
但是,如果您关闭设备,数据将保持不变,直到您将其删除(见下文)或重新刷新设备。
MicroPython on the micro:bit provides a flat file system; i.e. there is no notion of a directory hierarchy, the file system is just a list of named files. Reading and writing a file is achieved via the standard Python open function and the resulting file-like object (representing the file) of types TextIO or BytesIO. Operations for working with files on the file system (for example, listing or deleting files) are contained within the os module.
micro:bit 上的 MicroPython 提供了一个平面文件系统;即没有目录层次结构的概念,文件系统只是一个命名文件的列表。读取和写入文件是通过标准的 Python open 实现的 函数和生成的类文件对象(表示文件)的类型为 TextIO 或 BytesIO.用于处理文件系统上的文件的作 (例如,列出或删除文件)包含在 OS 模块。
If a file ends in the .py file extension then it can be imported. For example, a file named hello.py can be imported like this: import hello.
如果文件以 .py 文件扩展名结尾,则可以导入该文件。例如,可以按如下方式导入名为 hello.py 的文件:import hello。
An example session in the MicroPython REPL may look something like this:
MicroPython REPL 中的示例会话可能如下所示:
>>> with open('hello.py', 'w') as hello:
... hello.write("print('Hello')")
...
>>> import hello
Hello
>>> with open('hello.py') as hello:
... print(hello.read())
...
print('Hello')
>>> import os
>>> os.listdir()
['hello.py']
>>> os.remove('hello.py')
>>> os.listdir()
[]
Returns a file object representing the file named in the argument filename. The mode defaults to 'r' which means open for reading in text mode. The other common mode is 'w' for writing (overwriting the content of the file if it already exists). Two other modes are available to be used in conjunction with the ones describes above: 't' means text mode (for reading and writing strings) and 'b' means binary mode (for reading and writing bytes). If these are not specified then 't' (text mode) is assumed. When in text mode the file object will be an instance of TextIO. When in binary mode the file object will be an instance of BytesIO. For example, use 'rb' to read binary data from a file.
返回一个文件对象,该对象表示参数中命名的文件 filename 的模式默认为 'r',表示在文本模式下打开以供读取。另一种常见模式是 'w' 用于写入(如果文件已经存在,则覆盖文件的内容)。另外两种模式可以与上述模式结合使用: 't' 表示文本模式(用于读取和写入字符串),'b' 表示二进制模式(用于读取和写入字节)。如果未指定这些值,则 't' (文本模式)。当处于文本模式时,文件对象将是一个 TextIO 的实例。在二进制模式下,文件对象将是 BytesIO 的实例。例如,使用 'rb' 从文件中读取二进制数据。
Instances of these classes represent files in the micro:bit’s flat file system. The TextIO class is used to represent text files. The BytesIO class is used to represent binary files. They work in exactly the same except that TextIO works with strings and BytesIO works with bytes.
这些类的实例表示 micro:bit 的平面文件系统中的文件。TextIO 类用于表示文本文件。BytesIO 类用于表示二进制文件。它们的工作方式完全相同,只是 TextIO 处理字符串,而 BytesIO 处理字节。
You do not directly instantiate these classes. Rather, an appropriately configured instance of the class is returned by the open function described above.
您不直接实例化这些类。相反,上述 open 函数返回该类的适当配置实例。
Flush and close the file. This method has no effect if the file is already closed. Once the file is closed, any operation on the file (e.g. reading or writing) will raise an exception.
刷新并关闭文件。如果文件已关闭,则此方法无效。文件关闭后,对文件的任何作(例如读取或写入)都将引发异常。
Returns the name of the file the object represents. This will be the same as the filename argument passed into the call to the open function that instantiated the object.
返回对象表示的文件的名称。这与传递给 open 的调用中的 filename 参数相同 实例化对象的函数。
Read and return at most size characters as a single string or size bytes from the file. As a convenience, if size is unspecified or -1, all the data contained in the file is returned. Fewer than size characters or bytes may be returned if there are less than size characters or bytes remaining to be read from the file.
读取并返回大多数大小的字符作为单个字符串或 size 字节。为方便起见,如果未指定 size 或 -1,则返回文件中包含的所有数据。如果剩余要从文件中读取的字符或字节数小于 size,则可能会返回小于 size 的字符数或字节数。
If 0 characters or bytes are returned, and size was not 0, this indicates end of file.
如果返回 0 个字符或字节,并且 size 不是 0,则表示文件结束。
A MemoryError exception will occur if size is larger than the available RAM.
如果大小大于可用 RAM,则会发生 MemoryError 异常。
Read characters or bytes into the buffer buf. If n is supplied, read n number of bytes or characters into the buffer buf.
将字符或字节读取到缓冲区 buf 中。如果提供了 n,则将 n 个字节或字符读入缓冲区 buf。
Read and return one line from the file. If size is specified, at most size characters will be read.
从文件中读取并返回一行。如果指定了 size,则最多将读取 size 字符。
The line terminator is always '\n' for strings or b'\n' for bytes.
对于字符串,行终止符始终为 '\n' 或对于字节为 b'\n'。
Return True if the file supports writing. If False, write() will raise OSError.
如果文件支持写入,则返回 True。如果为 False, 则 write() 将引发 OSError 的 OS 的 Rror 中。
Write the string or bytes buf to the file and return the number of characters or bytes written.
将字符串或字节 buf 写入文件并返回写入的字符数或字节数。