python_文件操作

#字符编码:

#python 3默认读取数据是 utf-8

#windows默认是gbk编码

with open('test','r') as f:  #这个方法后面不需要关闭

file = open('test',encoding='utf-8').read()

#文件不能直接读取,因为是存在内存里面,如果是大文件会撑爆内存

file = open('test',encoding='utf-8')

for line in file:

   print(line,strip) #读取每一行数据

file.close  #关闭

# i.readline()

写  #r+ 读写,

#w+ 写读 ,先以写的模式打开,先删除原文件,然后在写入

 

 

#a+追加读

flie = open('test','w') #w= write 是创建模式,没有会创建,有也是创建

                               #a=append 追加模式,

file = write('你好中国')

flie = open('test','w') #   混合模式

 

flie.closed #判断文件是否删除

flie.enconding #打印文件的字符编码

flie.fileno() #

flie.flush #立马生效

flie.seek() #移动位置 横向移动

flie.truncate() #必须在 读的模式下打开,内容被清空,从光标的位置开始清空

 

posted @ 2019-12-01 10:36  小东阁下  阅读(150)  评论(0编辑  收藏  举报