文件IO读写操作### 1.文件I读写操作
open(file,mode)
r读出来的是字符串,rb读出来的是bytes
filename = log_file = r"E:\projecttest\files\file01.txt"
obj = open(filename,mode="r")
data = obj.read()
print(data)
print(type(data))
123456
qwertyui
wdwadeawewae
11111
222222
<class 'str'>
=====
filename = log_file = r"E:\projecttest\files\file01.txt"
obj = open(filename,mode="rb")
data = obj.read()
print(data)
print(type(data))
b'123456\r\nqwertyui\r\nwdwadeawewae\r\n11111\r\n222222'
<class 'bytes'>
1.读取模式
r模式
read()
- 按顺序 读到这里下次读从当前位置
#默认读取所有内容
filename = log_file = r"E:\projecttest\files\file01.txt"
data = open(filename,mode="r")
#test = data.read()
test1 = data.read(3) #读取前3
test2 = data.read(2) #在读取前3基础往后读2
test3 = data.read(5)
#print(test)
print(test1)
print(test2)
print(test3)
readline() 一行一行输出 ()有数字就输出x个字符
- 按顺序 读到这里下次读从当前位置
filename = log_file = r"E:\projecttest\files\file01.txt"
data = open(filename,mode="r")
line = data.readline()
line1 = data.readline()
line2 = data.readline(2)
print(line)
print(line1)
print(line2)
123456
qwertyui
wd
readlines() 每一行成一个列表
filename = log_file = r"E:\projecttest\files\file01.txt"
data = open(filename,mode="r")
line = data.readlines()
print(line)
['123456\n', 'qwertyui\n', 'wdwadeawewae\n', '11111\n', '222222']
Process finished with exit code 0
rb模式
seek(offset,whence) 在文件中移动光标
offset:偏移量
正数:向右移动
负数:向前移动
whence:
0:文件开始处
1:当前位置
2:文件结尾
filename = log_file = r"E:\projecttest\files\file01.txt"
obj = open(filename,mode="rb")
data = obj.read() #读文件一个字符一个字符 read默认读全部内容 所有光标就在最后了
data1 = obj.read()#这时再去读就没有内容了
print(data)
print(data1)
b'123456\r\nqwertyui\r\nwdwadeawewae\r\n11111\r\n222222'
b''
filename = log_file = r"E:\projecttest\files\file01.txt"
obj = open(filename,mode="rb")
data = obj.read() #光标到最后
print(obj.tell()) #读取当前的位置45
obj.seek(-3,2) #光标最后前移动3个
print(obj.tell()) #获取当前位置42
new_data = print(obj.read())
45
42
b'222'
2.写入模式
w,wb
注意:如果文件已存在打开的话会删除所有的内容 适用于新建文件
filename = r"E:\projecttest\files\file01.txt"
f_ob = open(filename,"w") #以写模式打开只能写 不能通过read再去读
f_ob.write("python\n")
f_ob.write("golang")
f_ob.flush() #刷新 不关闭 还能继续写操作
f_ob.close() #关闭 关闭后没法在改了
3.读写模式
r+ 替换操作
filename = r"E:\projecttest\files\file01.txt"
obj = open(filename,mode="r+") #把开头 五个w 替换成 123\n
obj.write("123\n")
wwwwww
eeeeee
123
w
eeeeee
4.追加模式
a 底部追加
filename = r"E:\projecttest\files\file01.txt"
f_obj = open(filename,mode="a")
f_obj.write("\nGolang\n")
f_obj.close()
python
shell
python
shell
Golang
for 循环遍历
filename = r"E:\projecttest\files\file01.txt"
obj = open(filename,mode="r")
#data = obj.read()
for i in obj:
print(i)
#文件
test
shell
cat
ls
#输出
test
shell
cat
ls
5.案例
案例1
从access日志中获取访问ip出现的次数
filename = r"E:\projecttest\files\access.log"
#定义列表
list = []
#定义字典
zidian = {}
name = open(filename,mode="r")
#拿到第一列的IP地址
for i in name:
data = i.split()[0]
list.append(data)
#print(list)
#遍历列表中的每个值;判断是否在字典中存在;不存在(字典中)赋初始值1;存在+1
for data in list:
if data not in zidian: #也可以用字典的keys方法
zidian[data] = 1
else:
zidian[data] += 1
#字典数据格式化输出
for i,j in zidian.items():
print("IP地址%s出现的次数为%s" % (i,j))
print(zidian)
for i in zidian:
print(i)
print(zidian.items())
IP地址124.128.58.67出现的次数为22
IP地址175.6.147.168出现的次数为3
IP地址121.5.79.247出现的次数为1
IP地址194.36.80.225出现的次数为1
{'124.128.58.67': 22, '175.6.147.168': 3, '121.5.79.247': 1, '194.36.80.225': 1}
124.128.58.67
175.6.147.168
121.5.79.247
194.36.80.225
dict_items([('124.128.58.67', 22), ('175.6.147.168', 3), ('121.5.79.247', 1), ('194.36.80.225', 1)])
Process finished with exit code 0
案例2
实时跟踪错误日志,降错误日志写入新文件
思路:打开文件 移动光标最后 获取位置
import time
log_file = r"E:\projecttest\files\file01.txt"
new_log_file = r"E:\projecttest\files\file02.txt"
# 获取文件最后的光标位置
obj = open(log_file,mode = "rb") #rb模式支持光标的移动 python3
obj.seek(0,2) #0代表光标不动 如果不为0 为x x为正数;则表示从最后向后移动x x为负数;则表示从最后向前移动x
old_location = obj.tell() #读当前光标位置
#print(old_location)
new_obj = open(new_log_file,mode="w")
#循环 移动光标 判断位置是否有心内容
while True:
obj.seek(0,2)
new_location = obj.tell()
if new_location > old_location:
offset = old_location - new_location
obj.seek(offset,2)
old_location = new_location
#print(obj.read())
#print(type(obj.read()))
line = obj.read()
#判断行里是否存在error,有的话写入新文件nwe_log_file
if b"error" in line:
new_obj.write(str(line) + "\n")
new_obj.flush()
time.sleep(1)
# 注意判断是否存在时 判断的最好是变量名 变量里存放内容 不要直接将函数类似 obj.read()直接作为变量值 这里就会有问题
Comments NOTHING