python 多次读取文件的细节

 file = open("test.txt", encoding="utf8")    #文档以utf8编码读取,不然默认gbk,中文会出现乱码

 data = file. read()

 data2 = file.read()

   print(data2)  #结果为空,第一次读完指针就停留在末尾,第二次读接着上次的指针的位置,所以没有内容可以读取

 

默认打开是只读模式

file = open("test.txt", "w", encoding="utf8")  #w指定为编辑模式,但是不能读,若文件存在则会覆盖

file.write("this is a test")

追加模式

file = open("test.txt", "a", encoding="utf8")  #a是append的意思

posted @ 2018-10-10 11:37  jeff9571  阅读(1679)  评论(0编辑  收藏  举报