Python9-文件操作-day8

# 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'xe5x93x88xe5x93x88xe5x93x88xe5x93x88'

 
#只写: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('{}
{}'.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)
原文地址:https://www.cnblogs.com/zhangtengccie/p/10223847.html