文件操作有三种模式:读模式(read)、写模式(write)、追加模式(a)
三种模式对文件的操作有不同的用处,读模式一般用于获取文件内容,写模式用于将字符串写入文件,
写模式在写入新内容之前,总是会清空要写入文件的原有内容
追加模式也是用于将字符串写入文件,与写模式的区别就是,追加模式下,不会清空文件原有的内容
新内容在原文件内容的末尾开始写入
下面来看一下三种模式操作文件的代码
操作文件统一分三种:
1、打开一个已存在的文件,写点东西
2、打开一个不存在的文件,写点东西
3、读取文件
r 和 r+模式: 文件指针默认在文件起始位置
# r 模式:#1 打开不存在的文件会报错
f = open('r1','r', encoding='utf-8')
f.write('你好,大虫子')
f.close()
# r 模式: #2 不能写入,会报错
f = open('file1','r',encoding='utf-8')
f.write('你好,大虫子')
f.close()
# # r 模式::#3 可以读取文件
f = open('file1','r',encoding='utf-8')
print(f.read())
f.close()
# r+ 模式:#1 打开不存在的文件会报错
f = open('r1','r+', encoding='utf-8')
f.write('你好,大虫子')
f.close()
# r+ 模式: #2 可以写入,写入的字符串会替换掉第一行的相应长度的字符
f = open('file1','r+',encoding='utf-8')
f.write('abcd'.encode('utf-8').decode('utf-8-sig'))
f.close()
# r+ 模式::#3 可以读取文件
f = open('file1','r+',encoding='utf-8')
print(f.read())
f.close()
w 和 w+ 模式:
# w模式:#1 打开不存在的文件 创建新文件并写入
f = open('w1','w', encoding='utf-8')
f.write('你好,大虫子')
f.close()
# w 模式: #2 原有内容被清空,写入新内容
f = open('file1','w',encoding='utf-8')
f.write('你好,大虫子')
f.close()
# w 模式::#3 不可以读取文件
f = open('file1','w',encoding='utf-8')
print(f.read())
f.close()
# w+ 模式:#1 打开不存在的文件,创建新文件,写入内容
f = open('w+1','w+', encoding='utf-8')
f.write('你好,大虫子')
f.close()
# w+ 模式: #2 可以写入,原有内容被清空,写入新内容
f = open('file1','w+',encoding='utf-8')
f.write('abcd'.encode('utf-8').decode('utf-8-sig'))
f.close()
# w+ 模式::#3 读取文件,文件内容会被清空
f = open('w1','w+',encoding='utf-8')
print(f.read())
f.close()
a 和 a+ 模式 该模式打开时,文件指针默认在文件末尾
# 追加a 模式:#1 打开不存在的文件 会新建一个文件
f = open('r2','a', encoding='utf-8')
f.write('打开一个不存在的文件')
f.close()
# 追加a 模式: #2 可以写入,并且写入内容追加在原有内容末尾
f = open('file1','a',encoding='utf-8')
f.write('你好,大虫子\n')
f.close()
# 追加a 模式::#3 不可以读取文件
f = open('file1','a',encoding='utf-8')
print(f.read())
f.close()
# 追加a+ 模式:#1 打开不存在的文件会新建一个文件,并写入新内容
f = open('r1','a+', encoding='utf-8')
f.write('你好1,大虫子')
f.close()
# 追加a+ 模式: #2 可以写入,写入的内容追加在原有内容末尾
f = open('file1','a+',encoding='utf-8')
f.write('abcd')
f.close()
# 追加a+ 模式::#3 不可以读取文件,不会报错
f = open('file1','a+',encoding='utf-8')
print(f.read())
f.close()
如果要获取文件内容,一般使用r或r+模式,写入内容根据具体情况使用写模式或追加模式
文件指针
# 需求
# 监控日志,如果有攻击,封IP
# 分析
# 1.打开日志文件
# 2.获取日志文件的IP地址
# 3.判断每个IP出现的次数,如果大于100次的话,加入黑名单
# 4.每分钟读一次
# 文件句柄,文件对象
# 直接循环一个文件对象,每次循环的是文件的每一行
import time
point = 0 # 记录文件指针位置
while True:
ip_list = []
count = 0
f = open('access.log', encoding='utf-8')
f.seek(point) # 移动文件指针
# f.tell() # 获取到当前文件指针的位置
for line in f:
ip = line.split()[0]
ip_list.append(ip)
point = f.tell() # 记录文件指针的位置
ip_set = set(ip_list)
for iip in ip_set:
count = ip_list.count(iip)
if count > 100:
# print('ip出现大于100次的有:', iip)
print('一刀砍死黑客', iip)
f.close()
time.sleep(60) # 暂停60S
日志文件部分内容
178.210.90.90 - - [04/Jun/2017:03:44:13 +0800] "GET /wp-includes/logo_img.php HTTP/1.0" 302 161 "http://nnzhp.cn/wp-includes/logo_img.php" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4" "10.3.152.221"
178.210.90.90 - - [04/Jun/2017:03:44:13 +0800] "GET /blog HTTP/1.0" 301 233 "http://nnzhp.cn/wp-includes/logo_img.php" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4" "10.3.152.221"
178.210.90.90 - - [04/Jun/2017:03:44:15 +0800] "GET /blog/ HTTP/1.0" 200 38278 "http://nnzhp.cn/wp-includes/logo_img.php" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4" "10.3.152.221"
66.249.75.29 - - [04/Jun/2017:03:45:55 +0800] "GET /bbs/forum.php?mod=forumdisplay&fid=574&filter=hot HTTP/1.1" 200 17482 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "-"
37.9.169.20 - - [04/Jun/2017:03:47:59 +0800] "GET /wp-admin/security.php HTTP/1.1" 302 161 "http://nnzhp.cn/wp-admin/security.php" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4" "-"
37.9.169.20 - - [04/Jun/2017:03:48:01 +0800] "GET /blog HTTP/1.1" 301 233 "http://nnzhp.cn/wp-admin/security.php" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4" "-"
37.9.169.20 - - [04/Jun/2017:03:48:02 +0800] "GET /blog/ HTTP/1.1" 200 38330 "http://nnzhp.cn/wp-admin/security.php" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4" "-"
37.9.169.20 - - [04/Jun/2017:03:48:21 +0800] "GET /wp-admin/security.php HTTP/1.1" 302 161 "http://nnzhp.cn/wp-admin/security.php" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4" "-"
37.9.169.20 - - [04/Jun/2017:03:48:21 +0800] "GET /blog HTTP/1.1" 301 233 "http://nnzhp.cn/wp-admin/security.php" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4" "-"
37.9.169.20 - - [04/Jun/2017:03:48:23 +0800] "GET /blog/ HTTP/1.1" 200 38330 "http://nnzhp.cn/wp-admin/security.php" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4" "-"
42.236.49.31 - - [04/Jun/2017:03:49:04 +0800] "GET /questions HTTP/1.1" 200 41977 "http://bbs.besttest.cn/questions" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36; 360Spider" "-"
66.249.75.28 - - [04/Jun/2017:03:49:42 +0800] "GET /bbs/forum.php?mod=forumdisplay&fid=473&filter=digest&digest=1 HTTP/1.1" 200 17242 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "-"
123.125.71.60 - - [04/Jun/2017:03:52:50 +0800] "GET /robots.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" "-"
123.125.71.117 - - [04/Jun/2017:03:52:50 +0800] "GET /blog HTTP/1.1" 301 233 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" "-"
123.125.71.80 - - [04/Jun/2017:03:52:51 +0800] "GET /blog/ HTTP/1.1" 200 38330 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" "-"
66.249.75.28 - - [04/Jun/2017:03:53:29 +0800] "GET /bbs/forum.php?mod=forumdisplay&fid=516&filter=heat&orderby=heats HTTP/1.1" 200 17019 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "-"
40.77.167.135 - - [04/Jun/2017:03:55:07 +0800] "GET /static/css/bootstrap/fonts/glyphicons-halflings-regular.svg HTTP/1.1" 200 108738 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)" "-"