#-*- coding: utf-8 -*-
#测试文件名为:#text.txt#测试文件内容为:#abcdefg#每次操作后将文件复原
#r#以只读方式打开文件,文件不可写#要打开的文件不存在时会报错#文件的指针将会放在文件的开头#这是默认模式## file = open('test.txt', 'r')## FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'#file = open('text.txt', 'r')#print(file.read())## abcdefg#file.write('aaa')## io.UnsupportedOperation: not writable#file.close()
#rb#以二进制格式打开一个文件用于只读,文件不可写#要打开的文件不存在时会报错#文件指针将会放在文件的开头#这是默认模式## file = open('test.txt', 'rb')## FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'#file = open('text.txt','rb')#print(file.read())#b'abcdefg'## file.write(b'aaa')## io.UnsupportedOperation: not writable#file.close()
#r+#打开一个文件用于读写,写入内容为str#文件指针将会放在文件的开头#重新写入的内容从头开始替换#file = open('text.txt', 'r+')#file.write('aaa')#file.close()#file = o