程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Software use case in micropython kernel development notebook: file system experiment

編輯:Python

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

The contents of the manuscript Objective record
Contents
Basic experiments os Relevant command total junction Existing problems

 

  • The contents of this manuscript belong to MicroPython Kernel Development Notes : Experimental tasks are embedded in the book The content in .

Software use case :
This part of the manuscript includes :

  1. Read and write files .
  2. About os Related operations of .
  • Position in manuscript : Chapter VII relevant contents ;

 

§01 book Draft content


One 、 Basic experiments

1、 Text file write

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.')

2、 Text file reading

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 .

Two 、 os Relevant command

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']
>>>

 

※ total junction ※


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 .

One 、 Existing problems

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 :

  • MicroPython Kernel Development Notes : Experimental tasks are embedded in the book

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved