【Finish】Python Day 8

'''
1、文件操作
test.txt
1、文件路径:
2、编码方式:utf-8 gbk……
3、操作方式:只读、只写、追加、读写、写读……
以什么编码方式存储的文件,就以什么编码方式操作打开

只读:r
非文字类的文件/上传下载文件/图片等:rb
读写:r+
读写(字节方式):r+b

'''

# f = open('test.txt',mode='r',encoding='utf-8')
# content = f.read()
# print(content,type(content))
# f.close()

# 对于w,没有此文件,则创建文件;对于存在此文件,会将原来文件数据清空,重新写入
# f = open('log.txt',mode='w',encoding='utf-8')
# content = f.write('写权限 哦')
# f.close()

# # 对于wb,open('log.txt',mode='wb')不加编码方式
# f = open('log.txt',mode='wb')
# content = f.write('写权限123321'.encode('utf-8'))
# f.close()

# 追加:在原有数据的前提加放在最后追加
# f = open('log.txt',mode='a',encoding='utf-8')
# content = f.write('我是追击的数据')
# f.close()

# f = open('log.txt',mode='ab')
# content = f.write('4我是追击的数据'.encode('utf-8'))
# f.close()

# 先读后写
# f = open('log.txt',mode='r+',encoding='utf-8')
# print(f.read()) # 读完了,光标定位最后
# f.write('da xiao')
# f.close()

# 先写后读
# f = open('log.txt',mode='r+',encoding='utf-8')
# f.write('da') # 写完之后,默认定位在写结束的地方
# print(f.read())
# f.close()

# 按照字节类型进行读取写全
# f = open('log.txt',mode='r+b')
# print(f.read()) # 读完了,光标定位最后
# f.write('da xiao'.encode('utf-8'))
# f.close()

# 也是读写权限
# f = open('log.txt',mode='w+',encoding='utf-8')
# f.write('呵呵哒')
# f.seek(0) # 将光标调整到0的位置
# print(f.read())
# f.close()

# 追加:在原有数据的前提加放在最后追加
# f = open('log.txt',mode='a+',encoding='utf-8')
# f.write('我是追击的数据') # 将光标定位到追加数据的最后面
# # f.seek(0) # 将光标定位到最前面
# print(f.read())
# f.close()

# 功能详解
# f = open('log.txt',mode='r+',encoding='utf-8')
# # content = f.read() #读出来的都是字符
# f.seek(6) # 按照字节定光标的位置
# print(f.tell()) # 输出光标的位置
# content = f.read()
# print(content)
# f.close()

# f = open('log.txt',mode='a+',encoding='utf-8')
# content = f.write('我是追加的数据')
# count = f.tell() # 输出光标的位置
# f.seek(count-9)# 将最后9个字节相关数据输出
# print(f.read())
# f.close()

# f = open('log.txt',mode='r+',encoding='utf-8')
# f.readable() #是否可读
# line = f.readline() # 一行一行的读取
# line = f.readlines() # 每一行当成列表中的元素,添加到list
# f.truncate(6) # 对原文件截取相应个数
# for i in f:
# print(i)
# f.close()
# 知识点:读取文件不能全部一起读,建议一行一行或者一部分一部分读取,

# 自动关闭,无需填写close
# with open('log.txt',mode='r+',encoding='utf-8') as obj:
# print(obj.read())
#
# # 可以填写多个文件打开,使用with方式
# with open('log.txt', mode='r+', encoding='utf-8') as obj,\
# open('log.txt', mode='r+', encoding='utf-8') as obj2:

# 登录注册相关
# username = input('please input your username:')
# password = input('please input your password:')
# with open('list_of_info',mode='w',encoding='utf-8') as f:
# f.write('{}\n{}'.format(username,password))
# print('Register success')
#
# count = 0
# lst = []
# while count<3:
# username1 = input('please input your username:')
# password1 = input('please input your password:')
#
# with open('list_of_info',mode='r',encoding='utf-8') as f2:
# for line in f2:
# lst.append(line)
# if username1 == lst[0].strip() and password1 == lst[1].strip():
# print('Login success')
# break
# else:
# print('username or password is wrong')
# count+=1

# 2、编码
# 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)

posted on 2018-07-22 22:00  wonderful0714  阅读(147)  评论(0编辑  收藏  举报

导航