python学习day8

今日内容:

文件操作,以及编码

#文件的操作 r,rb,r+
# f = open('info',mode='r',encoding='utf-8')
# conent = f.read()
# print(conent,type(conent))
# f.close()
#对非文本类的文件进行操作,以字节的方式操作
# f = open('info',mode='rb')
# conent = f.read()
# print(conent,type(conent))
# f.close()
#读写模式r+ ,r+b读写bytes类型
# f = open('info',mode='r+',encoding='utf-8')
# print(f.read())
# f.write('读写测试')
# f.close()
# f = open('log',mode='r+b')
# print(f.read())
# f.write('大猪,小猪'.encode('utf-8'))
# f.close()

#写操作-->先将源文件内容删除,再写
#模式,只写(w)、(wb)
# f = open('log',mode='w',encoding='utf-8')
# f.write('sdasfdsf啊师傅')
# f.close()
#以字节的方式写
# f=open('log',mode='wb')
# f.write('发射点发生打发士大夫'.encode('utf-8'))
# f.close()
#w+模式
# f = open('log',mode='w+',encoding='utf-8')
# f.write('aaa')
# f.seek(0)
# print(f.read())
# f.close()

#追加模式a、ab
# f = open('log',mode='a',encoding='utf-8')
# f.write('追加')
# f.close()
# f = open('log',mode='ab')
# f.write('以b追加'.encode('utf-8'))
# f.close()

#文件的一些操作方法
f= open('log',mode='r+',encoding='utf-8')
# content = f.read(4) #都出来的每一个都是字符
# print(content)
# print(f.tell()) #查询光标所在位置,以字节为单位
# print(f.readable()) #是否可读
# line = f.readline() #读取一行
# print(line)
# line = f.readlines() #将每一行当作列表的的一个元素
# print(line)
#将f当作文件句柄,循环此文件,显示内容.
# for line in f:
#     print(line.strip())

#str --->byte  encode 编码
# s = '二哥'
# b = s.encode('utf-8')
# print(b)
# # #byte --->str decode 解码
# s1 = b.decode('utf-8')
# print(s1)


# s = 'abf'
# b = s.encode('utf-8')
# print(b)
# #byte --->str decode 解码
# s1 = b.decode('gbk')
# print(s1)

猜你喜欢

转载自www.cnblogs.com/wujunjie-sir/p/9183676.html