# 1、文件路径:d:\xxx.txt
# 绝对路径:从根目录开始
# 想对路径:当前文件下
# 2、编码方式:utf-8/gbk...
# 3、操作方式:只读。只写。追加、读写、写读.....
f=open('d:\mod.txt',mode='r',encoding='utf-8')
content=f.read()
print(content)
f.close()
# byte类型:rb
# byte类型:rb
f=open('公共',mode='rb')
content=f.read()
print(content)
f.close()

b'\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88'

 
#只写:w
f=open('log',mode='w',encoding='utf-8')
f.write('骑兵步兵')
f.close()
#现将源文件的内容删除,再写
f=open('log',mode='w',encoding='utf-8')
f.write('快看看')
f.close()

#wb

f=open('log',mode='wb')
f.write('快看看'.encode('utf-8'))
f.close()

追加

#追加
f=open('log',mode='a',encoding='utf-8')
f.write('骑兵步兵')
f.close()

ab

#ab
f=open('log',mode='ab')
f.write('骑兵步兵'.encode('utf-8'))
f.close()
#读写
f=open('log',mode='r+',encoding='utf-8')
content=f.read()
print(content)
f.write('big,small')
f.close()
#写读
f=open('log',mode='r+',encoding='utf-8')
f.write('python9')
content=f.read()
print(content)
f.close()
f=open('log',mode='r+b')
content=f.read()
print(content)
f.write('python9'.encode('utf-8'))
f.close()
#写读w+
f=open('log',mode='w+',encoding='utf-8')
f.write('python9')
print(f.read())
f.close()

#写读a+

f=open('log',mode='a+',encoding='utf-8')
f.write('bbbbb')
f.seek(0)
print(f.read())
f.close()

read

f=open('log',mode='r+',encoding='utf-8')
content=f.read(4)
print(content)
f.close()
f=open('log',mode='r+',encoding='utf-8')
# content=f.read(4) #read是按照字符定光标位置
f.seek(6) #seek是按照字节定光标位置的3/6/9. 一个中文是3个字节
content=f.read()
print(content)
f.close()

光标位置

f=open('log',mode='r+',encoding='utf-8')
f.seek(3)
print(f.tell()) #告诉你光标位置
# content=f.read()
# print(content)
f.close()
f=open('log',mode='a+',encoding='utf-8')
f.write('啥地方')
count = f.tell()
f.seek(count-9)
content=f.read()
print(content)
f.close()
f=open('log',mode='r+',encoding='utf-8')
print(f.readable()) #是否可读
print(f.writable()) #是否可写
line = f.readline() #一行一行读
line = f.readlines() #每一行当成列表中的一个元素,然后添加到列表中
f.truncate(2) #对原文件进行截取
print(line)
f.close()

循环

f=open('log',mode='r+',encoding='utf-8')
for i in f:
print(i)
f.close()

with

with open('log',mode='r+',encoding='utf-8') as f,\
open('log',mode='r+',encoding='utf-8') as f1:
print(f.read(1))
#三次登录验证
# 注册的用户名和密码写入到文件,打印,进入界面登录,
# 三次登录,从文件中读取用户名和密码验证
username = input("Register your username: ")
password = input("Register you password: ")
with open('register',mode='w',encoding='utf-8') as reg:
reg.write('{}\n{}'.format(username,password))
print('恭喜你,注册成功!!!!')
lis = []
i = 0
while i < 3:
usn = input("pls your username: ")
pwd = input("pls your password: ")
with open('register',mode='r+',encoding='utf-8') as reg1:
for line in reg1:
lis.append(line)
if usn == lis[0].strip() and pwd == lis[1].strip():
print('登录成功!!!')
break
else:print('密码错误!!!!!!')
i+=1
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)

最新文章

  1. iOS在线更新framework,使用NSBundle动态读取
  2. You don&#39;t have permission to access /index.php on this server
  3. UIImage类扩展返回一个带边框的圆形图片
  4. If you really want to compile without asm, configure with --disable-asm.
  5. loj 1357(树形dp)
  6. event 对象 小记
  7. Oracle- 日期格式和数字类型处理
  8. [LeetCode]题解(python):067-Add Binary
  9. JVM总结之命令行工具
  10. 【java】文件操作java.io.File
  11. SpringMVC(十一):SpringMVC 处理输出模型数据之SessionAttributes
  12. B20J_3231_[SDOI2014]旅行_树链剖分+线段树
  13. Java虚拟机三:OutOfMemoryError异常分析
  14. C#反射调用方法实例
  15. rxjs一句话描述一个操作符(1)
  16. java框架之Struts2(1)-简介及入门
  17. 【读书笔记】iOS-方法声明
  18. Linux运维笔记(一)网络基础知识
  19. in linux system of ftp command
  20. Centos调整时间时区

热门文章

  1. centOS 部署服务器(三)
  2. Dubbo端口占用错误信息
  3. python入门之运算符
  4. win10安装CAD后出现致命错误
  5. ABC时间管理法
  6. 一本通 1434:【例题2】Best Cow Fences
  7. hihocoder1776 序列
  8. jquery.qrcode.min.js生成二维码
  9. 最小化安装centos后ifconfig看不到eth0
  10. 在eclipse中查看你用的tomcat的路径