Jane Medium : This paper gives MicroPython Kernel Development Notes : Experimental tasks are embedded in the book File read / write operations and os Related software use case parts .
key word
:MicroPython,MM32F3277, File operations
Software use case :
This part of the manuscript includes :
- Read and write files .
- About os Related operations of .
- Position in manuscript : Chapter VII relevant contents ;
The following code is in the file text.txt Written by ‘*’ Triangle file formed by .
import machine
fname = 'text.txt'
with open(fname, 'w') as f:
for i in range(10):
f.write('*'*(i+1) + '\n')
print("Write text file.")
After code execution , Information display :
2
3
4
5
6
7
8
9
10
11
Write text file.
>>>
Every sentence f.write, This function returns the number of characters written to the file . If you do not want the program to be in f.write Output the number of characters written , have access to :
_ = f.write('*' * (i+1) + '\n')
The following procedure will 15 Line Yang Hui triangle value is written to the file yhtriangle.txt In file . The reading results are shown below Text file reading experiment .
import machine
LINE_NUM = 15
b = [1]
fname = 'yhtriangle.txt'
with open(fname, 'w') as f:
for i in range(LINE_NUM):
strall = ' '.join([str(s) for s in b]) + '\n'
f.write(strall)
b = [1] + [b[n]+b[n+1] for n in range(len(b)-1)] + [1]
print('Write text file end.')
The following code will text.txt Each line in the file is read in , And show it .
fname = 'text.txt'
print("Text file contents:")
with open(fname, 'r') as f:
for l in f.readlines():
print(l.strip('\n'))
Program run results :
Text file contents:
*
**
***
****
*****
******
*******
********
*********
**********
>>>
The following code is to convert the above fname Modified into yhtriangle.txt after , Read the contents of Yang Hui's triangle file .
Text file contents:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
>>>
Because the types returned from file reading are str , So you can't read binary files directly .
Let's test through interactive operation os Related functions . Enter the following command in the interactive window .
import os
dir(os)
os.listdir('')
MicroPython The output is :
['__name__', 'remove', 'chdir', 'getcwd', 'ilistdir', 'listdir', 'mkdir', 'rename', 'rmdir', 'stat', 'statvfs', 'sync', 'unlink']
['System Volume Information', 'test.txt', 'test1.py', 'data.dat', 'mm32sub.py', 'text.txt', 'subfile.py', 'i2coled.py', 'yhtriangle.txt', 'main.PY', 'gif.txt']
>>>
The first line shows os Functions that can be used in . The second line is to call os.listdir() function , Show SD All files in the Kagan Directory . You can see that the text.txt , yhtriangle.txt Equal text file .
Use os.remove command , You can delete SD Relevant documents in the card . For example, use
os.remove('text.txt')
Can will SD In the card text.txt File deletion . Reuse os.listdir() The results are as follows . among text.txt It has been deleted .
['System Volume Information', 'test.txt', 'test1.py', 'data.dat', 'mm32sub.py', 'subfile.py', 'i2coled.py', 'yhtriangle.txt', 'sub1', 'main.PY', 'gif.txt']
>>>
This paper gives MicroPython Kernel Development Notes : Experimental tasks are embedded in the book File read / write operations and os Related software use case parts .
Use f.read() Read binary , Always return yes str Data objects . This is the problem of reading data from binary files . Now pass the test , There is still no way to read or write binary files .
■ Links to related literature :