3.15 haas506开发教程-example-文本操作
- 1.文本读取/写入
1.文本读取/写入
- main.py
# coding=utf-8
# @file main.py
# @author wyk
# @version V01
# @date 2021.11.4
# @brief main code 0f sd_read_write
# /sdcard0/filename1.txt
'''
1表示 可以,0表示不行
_____________________________________________
模式 r r+ w w+ a a+
读 1 1 0 1 0 1
写 0 1 1 1 1 1
创建 0 0 1 1 1 1
覆盖 0 0 1 1 0 0
指针在头 1 1 1 1 0 0
指针在尾 0 0 0 0 1 1
_____________________________________________
'''
import uos
def main():
#启用sd卡
uos.mountsd()
#查看sd卡中有什么文件
print("files in sd card are:",uos.listdir('/sdcard0'))
read_from_txt()
print('-'*30)
write_to_txt()
def read_from_txt():
#sd卡中已经有filename1.txt,读取他的内容
print('read_from_txt test')
f=open('/sdcard0/filename1.txt','r')
#读取指定一行的内容
content1=f.readline()
print('content :',content1)
f.close()
def write_to_txt():
#1.将内容写入txt文件中(当sd卡中没有filename2.txt时,w模式会创建该文件)
#2.然后再读取txt文件内容
print('write_to_txt test')
f=open('/sdcard0/filename2.txt','w')
lens=f.write("type anything you want")
#显示写入文件的字符数
print("lens of content:",lens)
f.close()
print('-'*30)
p=open('/sdcard0/filename2.txt','r')
c=p.read(100)
print('content in filename2.txt is:',c)
p.close()
if __name__ == '__main__':
main()
- 结果
files in sd card are: ['record1.pcm', 'System Volume Information', 'haas506.mp3', 'helloworld.mp3', 'filename1.txt', 'filename2.txt']
read_from_txt test
content : welcome come to use haas 506
------------------------------
write_to_txt test
lens of content: 22
------------------------------
content in filename2.txt is: type anything you want