python学习-day3 文件操作

*爱你&永不变心* 提交于 2019-12-05 06:21:37

day3

1.文件读写

2.函数

文件操作

r read 只读,不写默认为r

w write 只写

a 追加,能写不能读,不会清空以前的内容

 

f = open('a.txt',’w',encoding='utf-8')

f.read()   #read直接读所有内容;

f.readlines()  #读取所有文件内容,返回一个list,元素是每行的数据看,大文件时不要用,会把文件内容都读到内存中

f.readline() #每次读一行内容

f.close() #关闭文件

 

#文件指针

f = open('b.txt','w')

f.write('abcdcde\n')   #只能写字符串

f.writelines(['abc','12344'])  #writelines可以写list,或者集合,自动循环读取每个元素

f.close()

 

r  能读,不能写,打开不存在的文件会报错

w  能写,不能都,清空以前的内容

a  能写,不会清空以前的内容,不能读,追加

 

r+ 读写模式

w+ 写读模式

a+ 追加读模式

 

#r+模式,能读能写,打开不存在的文件会报错

#read()和write()位置不同,先读和后读位置不一样

f = open('a.txt','r+',encoding='utf-8)

print(f.read())  #文件指针读完到最后

f.write('r+模式111')   

f.close()   

 

#w+模式,能读能写,会清空以前的内容

#先read读后write写读不到内容

f = open('a.txt','w+',encoding='utf-8)

f.write('w+模式') 

print(f.read())    

f.close()   

 

#a+模式,读不到内容,因为文件指针位置在末尾,不清空内容,直接末尾追加内容

f = open('a.txt','a+',encoding='utf-8)

f.seek(0) #移动文件指针到最前面

print(f.read())    

f.write('a+模式222') 

f.close()   

 

 

监控服务器日期代码如下:

日志文件access.log

e.g.监控服务器日志,找出每分钟访问超过100次的IP地址,限制

思路:1.读取文件,获取文件的所有IP地址read 或readlines,按照空格分隔,找到第0个元素

  2.把ip地址存起来,用字典or列表?用字典存储占用内存少,key是ip地址,value是次数

    d= {'192.168.1.2':次数,'192.168.1.5':次数}

  3.循环字典,判断value是否超过100

import time

while true: #一直监控,死循环

ips = {} #存放所有的ip地址以及出现的次数

f = open('access.log')

for line in f:

#line.split()[0]  避免文件中有空行,先判断一下

if line.strip()!='': #判断非空行

ip = line.split()[0]

if ip not in ips:

ips[ip] = 1

else:

ips[ip]+ = 1

for ip in ips:

if ips.get(ip) >= 100:

print('超过100次的ip地址是:%s'%ip)

 

time.sleep(60) #间隔60s读一次文件,当前读前一分钟的文件

!!上面的代码,会出现重复读取文件数据,第二次循环会读取第一次读过得内容

import time

point = 0

while true:

ips = {}

f = open('access.log')

f.seek(point)

for lin in f: 

if line.strip()!='':

ip = line.split()[0]

if ip not in ips:

ips[ip]= 1

else:

ips[ip]+=1

point = f.tell() #当前文件指针的位置

for ip in ips:

if ips.get(ip)>=100:

print('超过100次的ip地址是:%s'%ip)

time.sleep(60)

 

 

 

单个文件操作:with 使用完文件句柄之后,自动关闭该文件

with open('file.txt','r') as f: #打开一个文件,把这个文件的句柄赋给f

for line in f:

print(line) #line就是每一行文件的内容

 

多个文件的操作:fr是读file.txt,fw是新建一个file_bak文件

with open('file.txt','r) as fr,with open('file_bak','w') as fw:

for lin in fr: #循环file.txt中的每一行

fw.write(line) #写道file_bak中

 

--------------------------------修改文件----------------------------------------------

方式1.把文件的全部内容读取到内存中,把原文件清空,重新写新的内容;

with open('file.txt','r+') as fr:

res = fr.read()

new_res = fr.replace('我','me')

fr.write(new_res)

方式2:把修改后的文件内容写到一个新的文件中

with open('file.txt') as fr,with open('new_file','w') as fw:

for line in fr:

new_line = fr.replace('我','me')

fw.write(new_line)

 

 

------------------------------------------集合set----------------------------------------------

集合set 类似list的一种数据类型,无序,去重

set作用:去重,可以关系测试,定义集合(交集,并集,差集)

list = [1,2,3,4,5,5,3]

s_list = set(list)  #定义集合

set1 = set([1,2,3,4,5,5,3])

set2 = {'haha','heihei','hehe'} #直接定义集合

 

集合操作:

list1 = {1,2,3,4,5]}

list2 = {2,4,6}

list3 = {3,4,5,7}

list1.add(888) #添加元素

list1.update([777,111,2]) 

list1.remove(777) #删除元素,如果元素不存在,会报错

list1.pop() #删除元素,并返回删除的元素

list1.discard('ddd') #如果删除的元素存在,就删除,不存在不做处理

 

print(list1.intersection(list2)) #取交集

print(list1&list2) #取交集

 

print(list1.union(list2)) #取并集

print(list1|list2) #取并集

 

print(list1.difference(list2)) #取差集

print(list1-list2)

 

print(list3.issubset(list1)) #判断list3是不是list1的子集

print(list1.issuperset(list3)) #判断list1是不是list3的父集

 

print(list1.isdisjoint(list3)) #判断list1和list3是否有交集

print(list1.symmetric_difference(list2)) #对称差集,输出两个list中都没有的值,去掉两个集合中相同的值

print(list1^list2) #对称差集

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!