文件的读操作
示例:
 print("->文件句柄的获取,读操作:")
 f = open('无题','r',encoding='utf8')
 d = f.read()
 f.close()
 print(d)
 print('->例二:')
 f = open('无题','r',encoding='utf8')
 e = f.read(9)
 f.close()
 print(e)
 #python3中,文件中一个中英文都占位1
运行结果:
复制代码
->文件句柄的获取,读操作: 昨夜星辰昨夜风 画楼西畔桂堂东 身无彩凤双飞翼 心有灵犀一点通 ->例二: 昨夜星辰昨夜风 画
文件的写操作
知识点:
1. 写操作前,文件先格式化清空文件
2.清空操作,在执行open的w方法后,清空
print("写的操作,写文件的时候,不能调用读方法,读文件的时候,不能调用写方法")
f = open('python','w',encoding='utf8')
f.write("I must learn python nbecause, python is important n")
f.write("java is better?")
f.write("maybe") #上面的语句,没有加换行符,所以输出的内容是紧接的
f.close()
运行结果:
打开文件后显示如下
I must learn python because, python is important java is better?maybe
文件的append方法
语法格式:
f = open('文件名','a','encoding = utf8')
文件这种方法为追加模式:1, 空白文件中,直接从头开始写入内容; 2 有内容的文件,会在末尾开始继续写入内容
示例:
f = open('python','a',encoding='utf8')
f.write("花开又花落")
f.close()
运行结果:
I must learn python because, python is important java is better?maybe花开又花落
readline 和 readlines
readline是逐行读取
readlines是全文读取
示例:
 print("readline方法")
 f = open('无题','r',encoding='utf8')
 a = f.readline()
 print("此时光标位置:",f.tell())
 b = f.readline()
 print("此时光标位置:",f.tell())
 print(a.strip()) #strip是字符串方法中去除空格和换行的方法
 print(b.strip())
 print("readlines方法,会将每行的内容组成一个列表打印")
 f = open('无题','r',encoding='utf8')
 c = f.readlines()
 print(c)
 print(id(c))
 print(id(f))
 for i in c:
  print(i.strip())
 print("遍历方法")
 f.seek(0)
 for i in f:
  print(i.strip())
 f.close() #文件的操作中,close()方法一定不能忘记
运行结果:
readline方法 此时光标位置: 23 此时光标位置: 46 昨夜星辰昨夜风 画楼西畔桂堂东 readlines方法,会将每行的内容组成一个列表打印 ['昨夜星辰昨夜风n', '画楼西畔桂堂东n', '身无彩凤双飞翼n', '心有灵犀一点通'] 37826824 5344280 昨夜星辰昨夜风 画楼西畔桂堂东 身无彩凤双飞翼 心有灵犀一点通 遍历方法 昨夜星辰昨夜风 画楼西畔桂堂东 身无彩凤双飞翼 心有灵犀一点通
文件的tell() 和 seek()方法
示例:
f = open('无题','r',encoding='utf8')
f.read(4)
print('当前光标位置',f.tell())
f.seek(10)
print('当前光标位置',f.tell())
f.close()
#read时,一个中文算三个字符
运行结果:
当前光标位置 12
当前光标位置 10
文件操作之flush方法
import sys,time
for i in range(20):
 sys.stdout.write("#")
 sys.stdout.flush()
 time.sleep(1)
truncate方法
f = open('test','w')
f.write("hello")
f.write("n")
f.write("python")
f.flush() #这样不用执行close方法,内存中的数据,就会写入到disk
f.close()
f = open('test','a')
f.truncate(2) #截断方法,光标从2开始往后截取
f.close()
其他的文件方法: r+ 读写方法
基于字符read & write
最基本的文件操作当然就是在文件中读写数据。这也是很容易掌握的。现在打开一个文件以进行写操作:
fileHandle = open ( 'test.txt', 'w' )
‘w'是指文件将被写入数据,语句的其它部分很好理解。下一步就是将数据写入文件:
fileHandle.write ( 'This is a test.nReally, it is.' )
这个语句将“This is a test.”写入文件的第一行,“Really, it is.”写入文件的第二行。最后,我们需要做清理工作,并且关闭文件:
fileHandle.close()
正如你所见,在Python的面向对象机制下,这确实非常简单。需要注意的是,当你再次使用“w”方式在文件中写数据,所有原来的内容都会被删除。如果想保留原来的内容,可以使用“a”方式在文件中结尾附加数据:
fileHandle = open ( 'test.txt', 'a' ) fileHandle.write ( 'nnBottom line.' ) fileHandle.close()
然后,我们读取test.txt,并将内容显示出来:
fileHandle = open ( 'test.txt' ) print fileHandle.read() fileHandle.close()
以上语句将读取整个文件并显示其中的数据。
基于行的读写 line
fileHandle = open ( 'test.txt' ) print fileHandle.readline() # "This is a test." fileHandle.close()
同时,也可以将文件内容保存到一个list中:
fileHandle = open ( 'test.txt' ) fileList = fileHandle.readlines() for fileLine in fileList: print '>>', fileLine fileHandle.close()
或者在文件中一次读取几个字节的内容:
fileHandle = open ( 'test.txt' ) print fileHandle.read ( 1 ) # "T" fileHandle.seek ( 4 ) print FileHandle.read ( 1 ) # " "(原文有错)