day08 文件操作
一、文件操作
1、文件路径(绝对路径):d:\wo.txt
2、编码方式:utf-8,gbk,等
3、操作方式:只读、只写、追加、读写、写读
以什么编码方式储存的文件,就以什么编码打开进行操作。
二、操作方式
1、只读r
(1)
# 绝对路径 f = open('d:\。。。.txt',mode='r',encoding='UTF-8') content = f.read() print(content) f.close()
在pycharm中读取文件
f = open('。。。',mode='r',encoding='utf-8') content = f.read() print(content,type(content)) f.close()
(2)、rb 只读bytes类型 用于非文字类
f = open('。。。', mode='rb',) content = f.read() print(content) f.close()
(3)r+
读写(读出原来的文件内容,再将写的内容追加到原文件中)
f = open('log',mode='r+',encoding='utf-8') print(f.read()) f.write('大猛,小孟') f.close()
写读 (先写,从头开始替换文件内容,再读出没有被替换的内容)
f = open('log', mode='r+', encoding='utf-8') f.write('你我') print(f.read()) f.close()
r+b 读写(以bytes类型)
f = open('log',mode='r+b') print(f.read()) f.write('大猛,小孟'.encode('utf-8')) # 读出源文件的内容,再追加写的内容 f.close()
2、只写 w
(1)
# 对于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()
(2)wb 在pycharm中建立文件时要加编码utf-8才能读取
f = open('log',mode='wb') f.write('附近类似纠纷'.encode('utf-8')) # f.close()
在文件中追加
f = open('log',mode='a',encoding='utf-8') f.write('佳琪') f.close()
(3)w+
写读:先将原文件的内容清除后,再写,(读出来的什么内容都没有显示)
f = open('log',mode='w+',encoding='utf-8') f.write('aaa') print(f.read()) f.close()
调整光标
f = open('log',mode='w+',encoding='utf-8') f.write('aaa') f.seek(0) #将光标调整到0的位置 print(f.read()) f.close()
(4)w+b
f = open('log',mode='w+b') f.write('我和你'.encode('utf-8')) f.seek(0) #将光标 的位置调整到0,因此可以输出文件内容 print(f.read()) f.close()
3、追加 a
(1)
f = open('log',mode='a',encoding='utf-8') f.write('佳琪') f.close() f = open('log',mode='a+',encoding='utf-8') f.write('佳琪') f.seek(0) # 调整光标位置,可以读出文件内容 print(f.read()) f.close()
(2) ab bytes型
f = open('log',mode='ab') f.write('佳琪'.encode('utf-8')) #只能写不能读 f.close()
三、功能详解
1.read
f = open('log',mode='a+',encoding='utf-8') content = f.read(3) #按字符去读 print(content) f.close()
2.write
3.seek 定光标的位置
f = open('log',mode='a+',encoding='utf-8') f.seek(3) #按字节定光标的位置 content = f.read() print(content) f.close()
4.tell 告诉你光标的位置
f = open('log',mode='a+',encoding='utf-8') f.seek(3) #按字节定光标的位置 print(f.tell()) f.close()
f = open('log',mode='a+',encoding='utf-8') f.write('佳琪') count = f.tell() #告诉光标的位置 f.seek(count-9) #读取后三个符 print(f.read()) #读取结果 print(f.read(2)) #读取中间两个 f.close()
5. f.readable() # 是否可读
6. readline and readlines
f = open('log', mode='r+', encoding='utf-8') con = f.readline() # 一行一行读 print(con) f.close() f = open('log', mode='r+', encoding='utf-8') con = f.readlines() # 每一行当成列表中的一个元素,添加到list中 print(con) f.close()
7. truncate
f = open('log', mode='r+', encoding='utf-8') f.truncate(2) # 截取前两个 f.close()
8.循环输出
f = open('log', mode='r+', encoding='utf-8') for line in f: print(line) f.close()
9.with
with open('log', mode='r+', encoding='utf-8') as f: print(f.read()) # 打开文件 with open('log',mode='r+',encoding='utf-8') as f,\ open('log',mode='r',encoding='utf-8') as f1: print(f.read(),f1.read()) # 同时打开两个文件
四.用户注册登录 例子
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 = 0 while i < 3: 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('账号或密码错误') i+=1
改进:三次登录机会
文件操作太白作业
list = [] with open('F:\PycharmProjects\project1\day1\price_list.txt', 'r', encoding='utf-8') as file: for n in file: list2 = n.strip().split() dic = {'name': list2[0], 'price': list2[1], 'amount': list2[2]} list.append(dic) print(list) # 方法一:用for循环计算出总价格 sum = 0 for dic in list: sum = sum + int(dic['price']) * int(dic['amount']) print(sum) """ 方法二:用while循环实现计算总价格 i = 0 sum = 0 while i < len(list): sum = sum + int(list[i]['price']) * int(list[i]['amount']) i = i + 1 print(sum) """ # 第2题 #-*- coding:utf-8 -*- # author: wangning # QQ: 1198143315 # blog: http://blog.51cto.com/wn2100 # datetime:2018/6/13 11:34 import os with open('replace_exercise.txt') as read_file, open('.replace_exercise.txt.swap', 'w') as write_file: data = read_file.read() data = data.replace('alex', 'SB') write_file.write(data) os.remove('replace_exercise.txt') os.rename('.replace_exercise.txt.swap', 'replace_exercise.txt')

浙公网安备 33010602011771号