以前学习了传递列表,传递任意数量的实参,将函数储存在模块中,创建和使用类,使用类和实例,继承,导入类,以及Python标准库,以前进行了这些模块的学习,不过因为时间关系没能发博客,非常抱歉。
今天开一个全新的内容:从文件中读取数据,以及写入文件
1.从文件中读取数据
(1)读取整个文件
事先在python的对应文件夹里创建了一个名为zxc文件,然后使用代码进行文件内容的读取。
with open(‘zxc.txt’)as asd:
qwe=asd.read()
print(qwe)
123456789
1234578
6554884515
54984556
//成功输出
with
open(‘zxc.txt’)as asd:
qwe=asd.read()
print(qwe.rstrip())
123456789
1234578
6554884515
54984556 //剥夺末尾空白,再次成功输出。
(2)文件路径
dfg=‘D:\jkl\zxc.txt’ //表示出文件所在路径
with open(‘dfg’)as asd:
qwe=asd.read()
print(qwe.rstrip())
123456789
1234578
6554884515
54984556
(3)逐行读取
dfg=‘zxc.txt’
with
open(‘zxc.txt’)as asd:
qwe=asd.read()
print(qwe)
123456789
1234578
6554884515
54984556 /通过添加空白的方式,成功使结果按行输出
(4)创建一个包含文件各行内容的列表
dfg=‘zxc.txt’
with open(dfg)as asd:
zxc=asd.readlines()
for as in asd: print(zx.rstrip())
123456789
1234578
6554884515
54984556 //创建列表后成功输出