目录
1.读模式(默认模式) --- “ r ”##只读,不能编写,文件不存在报错
##文件的基本操作##
Ⅰ、访问文件操作过程
• 打开文件
• 读取文件
#######打开方式 打开文件-文件编码 • encoding: 文件的编码方式( str ) • encoding的默认值:None, 不同的平台打开的方式不一样 为什么需要编码 对于计算机来说,所有信息都是由0和1组成的二进制。人类无法仅用二进制就来完成计算机的各种操作而字符编码解决人与计算机之间的沟通问题。 #常见编码# 注:########读取方式 fp.readlines()
>>> fp = open("utf.txt")
>>> fp.read()
"'中文'\naaa\nbbb\nccc\n\n"
>>> fp.readlines()
[]
>>> fp = open("utf.txt")
>>> fp.readlines()
["'中文'\n", 'aaa\n', 'bbb\n', 'ccc\n', '\n']
>>> fp = open("utf.txt")
>>> fp.readlines()
["'中文' xz wyb\n", 'aaa\n', 'bbb\n', 'ccc\n', '\n']
>>>
utf.txt 文件内容 用fp.readlines()读取该文件发现文件中每一行内容作为一个列表输出。
eg:# 打开一个文件”example.txt“ # 打印文件内容
>>> fp = open("example.txt")
>>> print(fp.readlines())
['爱是一道光,如此美妙\n', '照亮我们,勇气的未来\n', '魔力北极光,传说的预言\n', '原来就是,恋人的眼光\n']
>>> print(fp)
<_io.TextIOWrapper name='example.txt' mode='r' encoding='UTF-8'>
>>> fp.close()
>>>
将信息读到内存
• 写入文件
• 关闭文件
保存文件并释放内存空间
Ⅱ、文件打开
一、open()
1.open 的五个常用参数
指定不同编码方式打开文件
默认编码打开方式为None,跟平台语音编码环境有关,平台是utf-8,这里就是utf-8 使用指定参数encoding 可以修改编码方式 open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) • file : 要打开的文件名( str ) • mode: 打开文件的方式( str ) => text, bytes • encoding: 文件编码方式(str) • errors: 当发生编码错误时的处理方式(str) ---- 'ignore'或'strict'(默认) • buffering: 缓存方式 ( int)
errors:
>>> fp_gbk = open("utf.txt",encoding = "utf-8")
>>> fp_gbk .read()
"'中文'\naaa\nbbb\nccc\n\n"
>>> fp_gbk = open("utf.txt",encoding = "gbk")
>>> fp_gbk .read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 3: illegal multibyte sequence
>>> fp_gbk2 = open("utf.txt",encoding = "gbk",errors = "ignore")
>>> fp_gbk2.read()
"'涓鏂'\naaa\nbbb\nccc\n\n"
>>>
2.windows utf-8 与linux utf-8文本
linux:
>>> fp = open("utf.txt")
>>> fp.read()
"'中文'\naaa\nbbb\nccc\n\n"
window:
>>> fp.read()
'中文\naaaInbbb ncccln'
>>>fp =open("win-utf.txt")
>>> fp.read()
Traceback(most recent call last):
File " <stdin> ",line 1,in <module>
File"/usr/lib64/python3.6/codecs.py",line 321 ,in decode
(result,consumed)= self. buffer decode(data,self.errors, final)
UhnicodeDecodeError:'utf-8'code can't decode byte Oxd6 in position 7:invalid continu
>>>fp = open("win-gbk.txt",encode = "gbk")
Traceback(most recent call last):
File"<stdin>".line 1,in <module>
TypeEror:'encode'is an invalid keyword arg ument for this function
>>>fp = open("win-gbk.txt",encoding="gbk")
>>>fp.read()
window 中文
————————————————
版权声明:本文为优快云博主「29号代码小白」的原创文章,遵循CC 4.0 BY-SA版权协议
原文链接:https://blog.youkuaiyun.com/qq_52077701/article/details/123737893
二、打开文件方式 -- mode
('r','w','x','a'任意选择一个,‘t’,'b'任意选择一个,‘+’是可选项)
1.读模式(默认模式) --- “ r ”
##只读,不能编写,文件不存在报错
##不可以write,只可以read
>>> fp = open("utf.txt")
>>> fp.write("zz")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
io.UnsupportedOperation: not writable
>>> fp.read()
"'中文' xz wyb\naaa\nbbb\nccc\n\n"
>>>
2.写模式
#存在写缓存,只有在缓冲区满或者文件对象关闭(fp.close()),或者程序退出的时候,才会写到磁盘。
① 写模式-- "w" 以覆盖方式写入,
>>>fp.close()
>>>fp = open("utf.txt","w")
>>> fp.read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
io.UnsupportedOperation: not readable
>>> fp.write("this is utf.txt")
15
>>> fp.close()
>>>>>> fp = open("utf2.txt","w")
>>> fp.write("this is utf2.txt")
16
>>>
②:写模式 -- “x” 文件存在时会报错,不存在时会创建新文件并写入。
>>> fp = open("utf2.txt","x")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileExistsError: [Errno 17] File exists: 'utf2.txt'
>>>
[root@bxsmm lianxi]# cat utf3.txt
this is utf3.txt[root@bxsmm lianxi]#
③ : 写模式 -- “a” 文件存在的情况下在原来内容后面追加写,文件不存在则创建新文件
>>> fp = open("utf3.txt","a")
>>> fp.write("this is utf3.txt a 模式")
21
>>> fp.close()
>>> fp = open("utf3.txt","r")
>>> fp.read()
'this is utf3.txtthis is utf3.txt a 模式'
>>>
3.文本模式
# “t” --》 text 默认模式
# “b” --> 二进制模式
4.读写模式
① : “ r+ ” 当文件存在时,可读写,并且是追加写,当文件不存在时报错。
>>> fp = open("utf3.txt","r+")
>>> fp.read()
'this is utf3.txtthis is utf3.txt a 模式'
>>> fp.write("a b c d e f g h ")
16
>>> fp.close()
>>> fp = open("utf3.txt","r+")
>>> fp.read()
'this is utf3.txtthis is utf3.txt a 模式a b c d e f g h '
>>> fp = open("utf4.txt","r+")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'utf4.txt'
>>>
②:“ w+ ” 当文件存在时,可读写,并且是将原来文件内容删除后进行写,当文件不存在时则会创建新的文件
>>> fp = open("utf3.txt","w+")
>>> fp.write("this is w+")
10
>>> fp = open("utf4.txt","w+")
>>> fp.write("aaa")
3
>>>