Python demo之文件读写操作

文件读写比较简单,这里就是起一个mark的作用。突出read 和readlines的区别。后者读到一个list中,前者直接读到全部内容。区别还是看一个demo:

 

outfile = open("lxyfiledemo.txt","w")
outfile.write("hello,this is just a test for writing file in python")
outfile.write("\n")
outfile.write("hi over")
outfile.close()

infile = open("lxyfiledemo.txt")
content = infile.read()
infile.close()
print(content)


infile2 = open("lxyfiledemo.txt")
infile2.seek(0)
infile2contentList = infile2.readlines()
infile2.close()
print(infile2contentList)

 

 

输出为:

 

hello,this is just a test for writing file in python

hi over

['hello,this is just a test for writing file in python\n', 'hi over']

 

说明:以上结果可以看到,read读到全部内容然后输出。

readlines读进一个list然后打印这个list.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值