只读/读取:
>>> re = open(r'C:\Users\Administrator\Desktop\z.txt',mode = 'r',encoding = 'utf-8') #文件路径可以是绝对路径,也可以是相对的,在复制路径时,注意开头的控制字符,路径前记得加R
>>> cont = re.read()
>>> cont
'\ufeffeuwyib\neswqdwqed\ndwedwe\ngerg\nerwgwtrg\nhth\nrhgrt\nhg'
>>> print(cont)
euwyib
eswqdwqed
dwedwe
gerg
erwgwtrg
hth
rhgrt
hg
>>> re.close()
#以bytes类型读取
>>> re.close()
>>> re = open(r'C:\Users\Administrator\Desktop\z.txt',mode = 'rb')
>>> cont = re.read()
>>> cont
b'\xef\xbb\xbfeuwyib\r\neswqdwqed\r\ndwedwe\r\ngerg\r\nerwgwtrg\r\nhth\r\nrhgrt\r\nhg'
>>> print(cont)
b'\xef\xbb\xbfeuwyib\r\neswqdwqed\r\ndwedwe\r\ngerg\r\nerwgwtrg\r\nhth\r\nrhgrt\r\nhg'
>>> re.close()
文件写入:
>>> wr = open(r'E:\zero.txt',mode = 'w',encoding = 'utf-8')
>>> wr.write('嘻嘻哈哈')
4
>>> wr.close()
>>> wr = open(r'E:\zero.txt',mode = 'r',encoding = 'utf-8')
>>> cont = wr.read()
>>> cont
'嘻嘻哈哈'
>>> wr = open(r'E:\zero.txt',mode = 'wb')
>>> wr.write('知易行难'.encode('UTF-8'))
12
>>> wr.close()
>>> re = open(r'E:\zero.txt',mode = 'r+b')
>>> cont = re.read()
>>> cont
b'\xe7\x9f\xa5\xe6\x98\x93\xe8\xa1\x8c\xe9\x9a\xbe'
同时读写:
#先读后写
>>> re = open(r'E:\zero.txt',mode = 'r+',encoding = 'utf-8')
>>> f = re.read()
>>> f
'知易行难'
>>> re.write('哈哈哈哈哈')
5
>>> re.close()
>>> re = open(r'E:\zero.txt',mode = 'r',encoding = 'utf-8')
>>> f = re.read()
>>> f
'知易行难哈哈哈哈哈'
#先写后读
>>> re = open(r'E:\zero.txt',mode = 'r+',encoding = 'utf-8')
>>> re.write('哈哈哈哈哈')
5
>>> re.read()
'哈哈哈哈'
>>> re.close()
#bytes类型读写
>>> re = open(r'E:\zero.txt',mode = 'r+b')
>>> re.read()
b'\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88'
>>> re.write('哈哈哈哈哈'.encode('utf-8'))
15
>>> re.close()
>>> re = open(r'E:\zero.txt',mode = 'r',encoding = 'utf-8')
>>> re.read()
'哈哈哈哈哈哈哈哈哈哈哈哈哈哈'
>>> re.close()
>>> re = open(r'E:\zero.txt',mode = 'r+b')
>>> re.read()
b'\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88'
>>> re.close()
写读:
>>> re = open(r'E:\zero.txt',mode = 'w+',encoding = 'utf-8')
>>> re.write('知易行难')
4
>>> re.read()
''
>>> re.tell() #输出光标位置
12
>>> re.seek(0) #调整光标位置
0
>>> re.read()
'知易行难'
>>> re.seek(0)
0
>>> re.read(2)
'知易'
>>> re.seek(0)
0
>>> re.read(3)
'知易行'
>>> re.close()
文件重命名:
>>> re = open(r'E:\zero.txt',mode = 'r',encoding = 'utf-8')
>>> re.read()
'知易行难'
>>> re.close()
>>> import os
>>> os.rename(r'E:\zero.txt',r'E:\zero1.txt')
>>> re = open(r'E:\zero.txt',mode = 'r',encoding = 'utf-8')
Traceback (most recent call last):
File "", line 1, in
re = open(r'E:\zero.txt',mode = 'r',encoding = 'utf-8')
FileNotFoundError: [Errno 2] No such file or directory: 'E:\\zero.txt'
>>> re = open(r'E:\zero1.txt',mode = 'r',encoding = 'utf-8')
>>> re.read()
'知易行难'
>>> re.close()
文件删除:
>>> re = open(r'E:\zero.txt',mode = 'r',encoding = 'utf-8')
>>> re.read()
'知易行难'
>>> re.close()
>>> import os
>>> os.remove(r'E:\zero.txt')
>>> re = open(r'E:\zero.txt',mode = 'r',encoding = 'utf-8')
Traceback (most recent call last):
File "", line 1, in
re = open(r'E:\zero.txt',mode = 'r',encoding = 'utf-8')
FileNotFoundError: [Errno 2] No such file or directory: 'E:\\zero.txt'