一.读取文件中各行的内容并存储到一个列表中 继续用resource.txt 举例 resource.txt my name is joker, I am 18 years old, How about you? test.py file_name = 'resource.txt' with open(file_name) as file_obj: lines = file_obj.readlines() print(lines) 控制台打印如下: 解释:可以看到在file_obj 用了readl
一.读取整个文件内容 在读取文件之前,我们先创建一个文本文件resource.txt作为源文件. resource.txt my name is joker, I am 18 years old, How about you? 如何读取文件全部内容,我们编写到reader.py文件中. reader.py with open('resource.txt') as file_obj: content = file_obj.read() print(content) 需要注意的是需要将resourc
TXT文件: txt是微软在操作系统上附带的一种文本格式,文件以.txt为后缀. 从txt文件中读取数据: with open ('xxx.txt') as file: data=file.readlines() 将数据写入txt文件: with open ('xxx.txt','a',encoding='utf-8') as file: file.write('xxxx') 注:a表示append,将数据一行行写入文件 JSON文件: JSON指JavaScript对象表示法(JavaScri