Python读取和写入文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python读取和写入文件相关的知识,希望对你有一定的参考价值。
#Read and Write from Files#
#coding=utf-8
import codecs
f = open("AccountList.txt","w")
L = u"张三\n李四\n王五\n赵六"
f.write(L)
f.close()
f = open("AccountList.txt","r")
print (f.readline()) #read lines one by one
print (f.readline())
print (f.readline())
print (f.readline())
f.seek(0)
print (f.readlines()) #Output lines as a list
import linecache
m = open("AccountList01.txt","w")
n = u"America\nChina\nJapan\nEngland\nCanda\nGermany"
m.write(n)
m.close()
#m = open("AccountList01.txt","r")
#f.seek(0)
print(linecache.getline("AccountList01.txt",1));
print(linecache.getline("AccountList01.txt",2));
print(linecache.getline("AccountList01.txt",3));
print(linecache.getline("AccountList01.txt",1));
print(linecache.getline("AccountList01.txt",4));
print(linecache.getline("AccountList01.txt",5));
print(linecache.getline("AccountList01.txt",6));
本文出自 “百草素心” 博客,请务必保留此出处http://suxin.blog.51cto.com/535505/1871521
以上是关于Python读取和写入文件的主要内容,如果未能解决你的问题,请参考以下文章