首页 / PYTHON / python 学习笔记 八
python 学习笔记 八
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python 学习笔记 八,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4827字,纯文字阅读大概需要7分钟。
内容图文
![python 学习笔记 八](/upload/InfoBanner/zyjiaocheng/756/eb6108def34d4063be76704b472d2e14.jpg)
1.文件操作
模特主妇护士老师.txt
1,文件路径:d:\模特主妇护士老师.txt
2,编码方式:uft-8 gbk ....
3,操作方式:只读,只写,追加,读写,写读
以什么编码方式储存的文件,就以什么编码打开进行操作
只读: r
rb
f = open('log',mode='r',encoding='utf-8')
content = f.read()
print(content)
f.close()
f = open('log',mode='rb')
content = f.read()
print(content)
f.close()
r+ 读写
r+b 读写 (以bytes类型)
f = open('log',mode='r+',encoding='utf-8')
content = f.read()
print(content)
f.write('\n我是r+方式写入的')
f.close()
f = open('log',mode='r+b')
content = f.read()
print(content)
f.write('\n我是r+b方式写入的'.encode('utf-8'))
f.close()
只写: w
wb
# 先将源文件的内容全部清除,再写
f = open('log',mode='w',encoding='utf-8')
f.write('我删除前面的内容再写进去')
f.close()
f = open('log',mode='wb')
f.write('我删除前面的内容再写进去1'.encode('utf-8'))
f.close()
w+
f = open('log',mode='w+',encoding='utf-8')
f.write('我还是先删除前面的内容再写进去')
f.seek(0)
print(f.read())
f.close()
w+b
f = open('log',mode='w+b')
f.write('我删除前面的内容再写进去1w+b'.encode('utf-8'))
f.seek(0)
print(f.read())
f.close()
追加 a
f = open('log',mode='a',encoding='utf-8')
f.write('\n我是追加进去的,前面加了回车')
f.close()
f = open('log',mode='ab')
f.write('\n我是追加进去的,前面加了回车,bytes'.encode('utf-8'))
f.close()
2,编码
# str --->byte encode 编码
# s = '二哥'
# b = s.encode('utf-8')
# print(b)
#byte --->str decode 解码
# s1 = b.decode('utf-8')
# print(s1)
# s = 'adf'
# b = s.encode('utf-8')
# print(b)
# # #byte --->str decode 解码
# s1 = b.decode('gbk')
# print(s1)
3,登录,注册
username = input('请输入你要注册的用户名:')
password = input('请输入你要注册的密码:')
with open('list_of_info',mode='w',encoding='utf-8') as f:
f.write('{}\n{}'.format(username,password))
print('恭喜您,注册成功!')
lis = []
i = 3
while i > 0:
usn = input('请输入你的用户名:')
pwd = input('请输入你的密码:')
with open('list_of_info',mode='r+',encoding='utf-8') as f1:
for line in f1:
lis.append(line)
if usn == lis[0].strip() and pwd == lis[1].strip():
print('登录成功!')
break
else:print('账号或密码错误,你有%s次机会!'%(i-1))
i -= 1
代码:
![python 学习笔记 八 - 文章图片](/upload/getfiles/0001/2021/5/4/20210504045458971.jpg)
![python 学习笔记 八 - 文章图片](/upload/getfiles/0001/2021/5/4/20210504045459104.jpg)
# f = open('d:\\123.txt',mode='r',encoding='utf-8') # content = f.read() # print(content) # f.close() # r # f = open('log',mode='r',encoding='utf-8') # content = f.read() # print(content) # f.close() # f = open('log',mode='r+',encoding='utf-8') # content = f.read() # print(content) # f.write('\n我是r+方式写入的') # f.close() # f = open('log',mode='r+b') # content = f.read() # print(content) # f.write('\n我是r+b方式写入的'.encode('utf-8')) # f.close() # rb # f = open('log',mode='rb') # content = f.read() # print(content) # f.close() # 先将源文件的内容全部清除,再写 # f = open('log',mode='w',encoding='utf-8') # f.write('我删除前面的内容再写进去') # f.close() # f = open('log',mode='w+',encoding='utf-8') # f.write('我还是先删除前面的内容再写进去') # f.seek(0) # print(f.read()) # f.close() # wb # f = open('log',mode='wb') # f.write('我删除前面的内容再写进去1'.encode('utf-8')) # f.close() # f = open('log',mode='w+b') # f.write('我删除前面的内容再写进去1w+b'.encode('utf-8')) # f.seek(0) # print(f.read()) # f.close() # a # f = open('log',mode='a',encoding='utf-8') # f.write('\n我是追加进去的,前面加了回车') # f.close() # ab # f = open('log',mode='ab') # f.write('\n我是追加进去的,前面加了回车,bytes'.encode('utf-8')) # f.close() # 功能详解 # f = open('log',mode='r+',encoding='utf-8') # print('文件名:',f.name) # # content = f.read(3) #读出来的都是字符 # f.seek(3) #是按照字节定光标位置 # # f.tell() #告诉你光标位置,返回int数值 # print(f.tell()) # content = f.read() # print(content) # f.readable() # 是否可读 # line = f.readline() # 一行一行的读 ''' i = 0 while i<2: i += 1 line = f.readline() if line: print(line.strip()) else:break ''' # line = f.readlines() #每一行当成列表的元素,添加到list中 ''' print(type(line),line) for li in line: print(type(li),li.strip()) ''' # f.truncate(20) # 在文件中取多少,就是多少,其他的删除 # for line in f: # print(line) # f.close() # f = open('log',mode='a+',encoding='utf-8') # f.write('佳琪') # count = f.tell() # f.seek(count-count) # print(f.read()) # f.close() """ with open('log',mode='r+',encoding='utf-8') as f,\ open('log2',mode='a+',encoding='utf-8') as f1,open('log3',mode='a+',encoding='utf-8') as f3: # f.read() # f.write('我要输入一个回车') # f1.read() # f1.seek(0) # print(f1.read()) # f1.write('我也要输入一个回车') # f.seek(0) # f1.seek(0) # f_1 = f.read() # f_2 = f1.read() # f3.truncate(0) # f3.write(f_1) # f3.write('\n') # f3.write(f_2) """ with open('美女.jpg', mode='r+b') as f1, \ open('美女1.jpg', mode='r+b') as f2, open('美女2.jpg', mode='r+b') as f3: # f_1 = f1.read() # f_2 = f2.read() # f3.write(f_1+f_2) # f3.write(f_2) f3.seek(0) f1.seek(0) print(f3.read()) print('\n') # print(f1.read())View Code
内容总结
以上是互联网集市为您收集整理的python 学习笔记 八全部内容,希望文章能够帮你解决python 学习笔记 八所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。