write(函数open()及其返回的文件对象的read、write等方法)

懵懂先生 网文资讯write(函数open()及其返回的文件对象的read、write等方法)已关闭评论104阅读模式

文章源自略懂百科-http://wswcn.cn/24513.html

主要内容:小目标:掌握文件基本操作主要内容:文章源自略懂百科-http://wswcn.cn/24513.html

文件基本操作;文章源自略懂百科-http://wswcn.cn/24513.html

文件读写;文章源自略懂百科-http://wswcn.cn/24513.html

编码格式;文章源自略懂百科-http://wswcn.cn/24513.html

如果看完这篇文章,你还是弄不明白文件; 你来找我,我保证不打你,我给你发100的大红包。文章源自略懂百科-http://wswcn.cn/24513.html

先来看下文件:文章源自略懂百科-http://wswcn.cn/24513.html

文件种类很多,txt,csv,excel等不同文件可以使用不同模块去进行操作文章源自略懂百科-http://wswcn.cn/24513.html

本文主要介绍文本文件操作。文章源自略懂百科-http://wswcn.cn/24513.html

1.文件操作基础来看下基本操作:打开,读写,关闭 代码实现:window下定义文件,使用r字符串fpath = rE:test.txtopen函数打开文件,返回文件对象f =open(fpath)read方法,将文件读完lines= f.read()print(lines)f.close()文章源自略懂百科-http://wswcn.cn/24513.html

结果,文件内容:文章源自略懂百科-http://wswcn.cn/24513.html

thisistest文章源自略懂百科-http://wswcn.cn/24513.html

看似简单,但是里面有很多知识点,我们来看下。文章源自略懂百科-http://wswcn.cn/24513.html

2. 文件打开文章源自略懂百科-http://wswcn.cn/24513.html

几个问题:文章源自略懂百科-http://wswcn.cn/24513.html

1. 以什么方式打开文章源自略懂百科-http://wswcn.cn/24513.html

2. 以什么编码格式打开文章源自略懂百科-http://wswcn.cn/24513.html

3. 打开会有什么结果文章源自略懂百科-http://wswcn.cn/24513.html

2.1 打开文件:open函数详解文章源自略懂百科-http://wswcn.cn/24513.html

open函数定义:文章源自略懂百科-http://wswcn.cn/24513.html

open( file,mode=r, buffering=-1, encoding=None, errors=None,newline=None,closefd=True,opener=None,)文章源自略懂百科-http://wswcn.cn/24513.html

主要参数:文章源自略懂百科-http://wswcn.cn/24513.html

open参数文章源自略懂百科-http://wswcn.cn/24513.html

2.2 mode详解 文章源自略懂百科-http://wswcn.cn/24513.html

文件打开方式文章源自略懂百科-http://wswcn.cn/24513.html

组合方式参数说明:文章源自略懂百科-http://wswcn.cn/24513.html

文件打开方式文章源自略懂百科-http://wswcn.cn/24513.html

二进制方式:wb,rb,wb+...;如上面操作类,需要加b选项2.3 写入文件,发生了什么使用w方式打开文件并写入汉字:测试window下定义文件,使用r字符串fpath = rE:t1.txtopen函数打开文件,返回文件对象wds =测试f =open(fpath,w)f.write(wds)print(f)f.close()文章源自略懂百科-http://wswcn.cn/24513.html

结果:文章源自略懂百科-http://wswcn.cn/24513.html

_io.TextIOWrapper name=E:t1.txtmode=wencoding=cp936文章源自略懂百科-http://wswcn.cn/24513.html

结果中可以看到:文章源自略懂百科-http://wswcn.cn/24513.html

文件路径:name文章源自略懂百科-http://wswcn.cn/24513.html

文件权限:w文章源自略懂百科-http://wswcn.cn/24513.html

文件编码格式:cp936,window下默认编码格式,linux下一般为utf-8文章源自略懂百科-http://wswcn.cn/24513.html

写入文件内容是什么?文章源自略懂百科-http://wswcn.cn/24513.html

1. 测试的编码格式为unicode,这个编码不能直接写入到f文件;文章源自略懂百科-http://wswcn.cn/24513.html

2. f.write写入时候,将wds进行编码,编码格式为cp936;文章源自略懂百科-http://wswcn.cn/24513.html

3. 编码完成之后,再写入文件;文章源自略懂百科-http://wswcn.cn/24513.html

4. 文件写有缓存,写入数据只是到了缓存中;什么时候同步到磁盘?文章源自略懂百科-http://wswcn.cn/24513.html

5. 情况1:只有缓存中的数据到了一定数量同步一次;文章源自略懂百科-http://wswcn.cn/24513.html

6. 情况2:调用flush方法强制写; 情况3:调用close方法关闭文件;文章源自略懂百科-http://wswcn.cn/24513.html

2.4 编码格式问题读取上一步写入的文件window下定义文件,使用r字符串fpath = rE:t1.txtopen函数打开文件,只读,编码格式为cp936f =open(fpath)print(f.read())f.close()文章源自略懂百科-http://wswcn.cn/24513.html

结果:测试文章源自略懂百科-http://wswcn.cn/24513.html

使用utf-8编码格式打开window下定义文件,使用r字符串fpath = rE:t1.txtopen函数打开文件,只读,编码格式为utf-8f =open(fpath, encoding=utf-8)print(f.read())f.close()文章源自略懂百科-http://wswcn.cn/24513.html

结果报错:文章源自略懂百科-http://wswcn.cn/24513.html

UnicodeDecodeError:utf-8codec cant decode byte0xb2 in position0: invalid start byte文章源自略懂百科-http://wswcn.cn/24513.html

纠正:使用文件对应的编码格式打开,window下cp936与gbk等通用;文章源自略懂百科-http://wswcn.cn/24513.html

遇到这种问题:打开文件编码格式与文件自身编码格式不匹配。文章源自略懂百科-http://wswcn.cn/24513.html

3. 文件读取方式文章源自略懂百科-http://wswcn.cn/24513.html

方法说明:文章源自略懂百科-http://wswcn.cn/24513.html

read方法文章源自略懂百科-http://wswcn.cn/24513.html

3.1 遍历文件方式1 :for 循环遍历,推荐这种方式:window下定义文件,使用r字符串fpath = rE:test.txtopen函数打开文件,返回文件对象f =open(fpath)逐行遍历文件forlineinf:print(line,end=)f.close()方式2:先将文件全部读出,然后逐行遍历window下定义文件,使用r字符串fpath = rE:test.txtopen函数打开文件,返回文件对象f =open(fpath)forlineinf.readlines():print(line,end=)f.close()3.2 如何理解:f.readline(size=-1, /)如果一行数据长度大于size,读取size个返回如果一行数据长度小于size,读取行尾返回fpath =rE:test.txtf = open(fpath)读取3个print(f.readline(3))f.close()f = open(fpath)读取10个print(f.readline(10))f.close()文章源自略懂百科-http://wswcn.cn/24513.html

结果:文章源自略懂百科-http://wswcn.cn/24513.html

thithis3.3 文件写入f.write(text, /):写入text,返回写入长度f.writelines(lines, /):一次写入多行f.flush():强制将缓存写入磁盘文件换行:n 示例:fpath =rE:t2.txtopen函数打开文件,返回文件对象wds = 测试f = open(fpath, w)f.write(wds)换行f.write(n)写入多行f.writelines([wds+n]*2)f.close()文章源自略懂百科-http://wswcn.cn/24513.html

结果:文章源自略懂百科-http://wswcn.cn/24513.html

测试测试测试4. seek操作文章源自略懂百科-http://wswcn.cn/24513.html

一个问题:文件读取之后,如何再次重新读取?文章源自略懂百科-http://wswcn.cn/24513.html

方式1:关闭文件,再次读取方式2:使用seek操作f.seek(cookie, whence=0, /):改变文件读写位置,cookie为偏移量whence为指定位置文章源自略懂百科-http://wswcn.cn/24513.html

whence说明0文件起始位置1文件当前位置,window只支持cookie=02文件结尾,window只支持cookie=0文章源自略懂百科-http://wswcn.cn/24513.html

读取为例:文章源自略懂百科-http://wswcn.cn/24513.html

fpath =rC:UsershygDesktopdatatest.txtf = open(fpath)读取3个line = f.read(3)print(line)将读取位置设文件开头,向后偏移一个字节f.seek(1,0)line = f.read(3)print(line)将读取位置设置到文件结尾f.seek(0, 2)line = f.read(3)print(line)文章源自略懂百科-http://wswcn.cn/24513.html

结果:文章源自略懂百科-http://wswcn.cn/24513.html

thihis文章源自略懂百科-http://wswcn.cn/24513.html

总结: 文件操作注意点:文章源自略懂百科-http://wswcn.cn/24513.html

打开方式,尤其适用w方式,注意文件会被清空读写二进制文件,需要使用rb或者wb方式读取文件推荐使用for遍历文件写入注意换行使用n文章源自略懂百科-http://wswcn.cn/24513.html

文章源自略懂百科-http://wswcn.cn/24513.html

懵懂先生
  • 本文由 发表于 2022年8月5日 10:45:35
  • 转载请注明:http://wswcn.cn/24513.html
网文资讯

养殖创业网(农村养殖创业项目推荐)

创业的项目有很多,但如今实实在在搞实业的并不多,尤其是能够赚到钱的案例更不多。现在在农村投资养殖项目,也是有很多的选择的,想要小本养殖创业项目并不是不可能。5万至10万元养殖创业项目推荐,下面具体来了...
网文资讯

碧螺春属于绿茶吗(碧螺春和龙井茶哪个好喝?)

碧螺春和西湖龙井都是中国名茶,同属于绿茶,但由于每个人的口味不一,因此对于碧螺春和龙井茶的喜好程度也不同,要说哪个好喝的话,这很难下定论,只能说说它们的区别。 认识碧螺春和龙井茶 碧螺春,是中国传统名...
网文资讯

为什么世界似乎被精心调整?

有没有一瞬间,你觉得整个世界像是被精心设计过的一样。在睡梦中发生的事,在现实生活中同样也会发生,难道人类拥有预知的能力,可以提前设计好以后发生的事情吗?有一句俗语说的就是冥冥之中自有天意,以前人类听命...
网文资讯

中俄能打过北约吗(中俄联盟能打过北约吗)

在美国和北约的压迫下,中国确实少了个知心朋友,因此,中俄关系对中国和俄罗斯两个国家来说的意义非常重大,中俄战略伙伴关系加大了双方对北约的抵抗力。那么中俄联手实力与北约相比到底如何呢。我们都知道上世纪九...