python 学习笔记 八 文件操作

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

代码:
# 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

 

posted @ 2019-07-17 19:55  休由  阅读(154)  评论(0编辑  收藏  举报