python 2.02 文件读写操作

1. 文件操作

在学习文件操作之前,先来回顾一下编码的相关以及先关数据类型的知识。

  • 字符串类型(str),在程序中用于表示文字信息,本质上是unicode编码中的二进制。

    name = "武沛齐"
    
  • 字节类型(bytes)

    • 可表示文字信息,本质上是utf-8/gbk等编码的二进制(对unicode进行压缩,方便文件存储和网络传输。)

      name = "武沛齐"
      data = name.encode('utf-8')
      print(data) # b'\xe6\xad\xa6\xe6\xb2\x9b\xe9\xbd\x90'
      
      result = data.decode('utf-8')
      print(result) # "武沛齐"
      
    • 可表示原始二进制(图片、文件等信息)

1.0 文件的路径不同的表现形式

  • python在描述路径时可以有多种方式,现列举常见的三种

    方式一:转义的方式
     
    'd:\\a.txt'  使用特殊符合\ 表示要转义符号本身的意义。
     
    方式二:显式声明字符串不用转义
     
    'd:r\a.txt'     r这个字符表示不转义
    r'D:\123.txt'
     
    方式三:使用Linux的路径``/
     
    'd:/a.txt'     直接使用linux系统/ 代表目录的间隔
    
  • 文件读写的前提

    1、要有 文件 路径+文件名
    
    2、字符编码格式 encoding
    
    3、换行符 lInux \n换行 
      windows \r\n 换行
    
    
    
    读写都是以str字符串为单位,一定要指定encoding
    

1.1 读文件

  • 读文本文件

    # 1.打开文件
    #	- 路径:
    #		相对路径:'info.txt'
    #		绝对路径:'/Users/wupeiqi/PycharmProjects/luffyCourse/day09/info.txt'
    # 	- 模式
    # 		rb,表示读取文件原始的二进制(r, 读 read;b, 二进制 binary;)
    # 1.打开文件
    file_object = open('info.txt', mode='rb')
    # 2.读取文件内容,并赋值给data
    data = file_object.read()
    # 3.关闭文件
    file_object.close()
    
    print(data) # b'alex-123\n\xe6\xad\xa6\xe6\xb2\x9b\xe9\xbd\x90-123'
    text = data.decode("utf-8")
    print(text)
    
    # 1.打开文件
    file_object = open('info.txt', mode='rt', encoding='utf-8')
    
    # 2.读取文件内容,并赋值给data
    data = file_object.read()
    
    # 3.关闭文件
    file_object.close()
    
    print(data)
    
  • 读图片等非文本内容文件。

    file_object = open('a1.png', mode='rb')
    data = file_object.read()
    file_object.close()
    
    print(data) # \x91\xf6\xf2\x83\x8aQFfv\x8b7\xcc\xed\xc3}\x7fT\x9d{.3.\xf1{\xe8\...
    

注意事项:

  • 路径

    • 相对路径,你的程序到底在哪里运行的?

    • 绝对路径

      # 1.打开文件
      file_object = open('/Users/wupeiqi/PycharmProjects/luffyCourse/day09/info.txt', mode='rt', encoding='utf-8')
      # 2.读取文件内容,并赋值给data
      data = file_object.read()
      # 3.关闭文件
      file_object.close()
      

      windows系统中写绝对路径容易出问题:

      # file_object = open('C:\\new\\info.txt', mode='rt', encoding='utf-8')
      
      file_object = open(r'C:\new\info.txt', mode='rt', encoding='utf-8')
      data = file_object.read()
      file_object.close()
      print(data)
      
  • 读文件时,文件不存在程序会报错。

    Traceback (most recent call last):
      File "/Users/wupeiqi/PycharmProjects/luffyCourse/day09/2.读文件.py", line 2, in <module>
        file_object = open('infower.txt', mode='rt', encoding='utf-8')
    FileNotFoundError: [Errno 2] No such file or directory: 'infower.txt'
    
    # 判断路径是否存在?
    import os
    
    file_path = "/Users/wupeiqi/PycharmProjects/luffyCourse/day09/info.txt"
    exists = os.path.exists(file_path)
    if exists:
        # 1.打开文件
        file_object = open('infower.txt', mode='rt', encoding='utf-8')
        # 2.读取文件内容,并赋值给data
        data = file_object.read()
        # 3.关闭文件
        file_object.close()
        print(data)
    else:
        print("文件不存在")
    

1.2 写文件

  • 写文本文件

    # 1.打开文件
    # 路径:t1.txt
    # 模式:wb(要求写入的内容需要是字节类型)
    file_object = open("t1.txt", mode='wb')
    
    # 2.写入内容
    file_object.write(    "武沛齐".encode("utf-8")    )
    
    # 3.文件关闭
    file_object.close()
    
    file_object = open("t1.txt", mode='wt', encoding='utf-8')
    
    file_object.write("武沛齐")
    
    file_object.close()
    
  • 写图片等文件

    f1 = open('a1.png',mode='rb')
    content = f1.read()
    f1.close()
    
    f2 = open('a2.png',mode='wb')
    f2.write(content)
    f2.close()
    

基础案例:

# 案例1:用户注册
"""
user = input("请输入用户名:")
pwd = input("请输入密码:")
data = "{}-{}".format(user, pwd)
file_object = open("files/info.txt", mode='wt', encoding='utf-8')
file_object.write(data)
file_object.close()
"""

# 案例2:多用户注册
"""
# w写入文件,先清空文件;再在文件中写入内容。
file_object = open("files/info.txt", mode='wt', encoding='utf-8')
while True:
    user = input("请输入用户名:")
    if user.upper() == "Q":
        break
    pwd = input("请输入密码:")
    data = "{}-{}\n".format(user, pwd)

    file_object.write(data)
file_object.close()
"""

小高级案例:(超前)

利用Python想某个网址发送请求并获取结果(利用第三方的模块)

  • 下载第三方模块

    pip install requests
    
    /Library/Frameworks/Python.framework/Versions/3.9/bin/pip3 install requests
    

    image-20201217152358142

  • 使用第三方模块

    import requests
    
    res = requests.get(url="网址")
    print(res)
    
# 案例1:去网上下载一点文本,文本信息写入文件。
import requests

res = requests.get(
    url="https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=20&page_start=20",
    headers={
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
    }
)

# 网络传输的原始二进制信息(bytes)
# res.content

file_object = open('files/log1.txt', mode='wb')
file_object.write(res.content)
file_object.close()



# 案例2:去网上下载一张图片,图片写入本地文件。
import requests

res = requests.get(
    url="https://hbimg.huabanimg.com/c7e1461e4b15735fbe625c4dc85bd19904d96daf6de9fb-tosv1r_fw1200",
    headers={
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
    }
)

# 网络传输的原始二进制信息(bytes)
# res.content

file_object = open('files/美女.png', mode='wb')
file_object.write(res.content)
file_object.close()

注意事项:

  • 路径
    • 绝对路径
    • 相对路径
  • 文件不存在时,w模式会新建然后再写入内容;文件存在时,w模式会清空文件再写入内容。

1.3 文件打开模式

上文我们基于文件操作基本实现了读、写的功能,其中涉及的文件操作模式:rt、rb、wt、wb,其实在文件操作中还有其他的很多模式。

========= ===============================================================
Character Meaning
--------- ---------------------------------------------------------------
'r'       open for reading (default)
'w'       open for writing, truncating the file first
'x'       create a new file and open it for writing
'a'       open for writing, appending to the end of the file if it exists

'b'       binary mode
't'       text mode (default)

'+'       open a disk file for updating (reading and writing)

The default mode is 'rt' (open for reading text).

关于文件的打开模式常见应用有:

  • 只读:rrtrb (用)

    • 存在,读
    • 不存在,报错
  • 只写:wwtwb(用)

    • 存在,清空再写
    • 不存在,创建再写
  • 只写:xxtxb

    • 存在,报错
    • 不存在,创建再写。
  • 只写:aatab【尾部追加】(用)

    • 存在,尾部追加。
    • 不存在,创建再写。
  • 读写

    • r+、rt+、rb+,默认光标位置:起始位置

      file_object = open('info.txt', mode='rt+')
      
      # 读取内容
      data = file_object.read()
      print(data)
      
      # 写入内容
      file_object.write("你好呀")
      
      file_object.close()
      
      file_object = open('info.txt', mode='rt+')
      
      # 写入内容
      file_object.write("alex")
      
      # 读取内容
      data = file_object.read()
      print(data)  # -123
      
      file_object.close()
      
    • w+、wt+、wb+,默认光标位置:起始位置(清空文件)

      file_object = open('info.txt', mode='wt+')
      
      # 读取内容
      data = file_object.read()
      print(data)
      
      # 写入内容
      file_object.write("你好呀")
      
      # 将光标位置重置起始
      file_object.seek(0)
      
      # 读取内容
      data = file_object.read()
      print(data)
      
      file_object.close()
      
    • x+、xt+、xb+,默认光标位置:起始位置(新文件)

    • a+、at+、ab+,默认光标位置:末尾

      file_object = open('info.txt', mode='at+')
      
      # 写入内容
      file_object.write("武沛齐")
      
      # 将光标位置重置起始
      file_object.seek(0)
      
      # 读取内容
      data = file_object.read()
      print(data)
      
      file_object.close()
      

多用户注册案例:

while True:
    user = input("用户名:")
    if user.upper() == "Q":
        break
    pwd = input("密码:")

    data = "{}-{}\n".format(user, pwd)

    file_object = open('files/account.txt', mode='a')
    file_object.write(data)
    file_object.close()
file_object = open('files/account.txt', mode='a')

while True:
    user = input("用户名:")
    if user.upper() == "Q":
        break
    pwd = input("密码:")
    data = "{}-{}\n".format(user, pwd)
    file_object.write(data)

file_object.close()

1.4 常见功能

在上述对文件的操作中,我们只使用了write和read来对文件进行读写,其实在文件操作中还有很多其他的功能来辅助实现更好的读写文件的内容。

  • read,读

    • 读所有【常用】

      f = open('info.txt', mode='r',encoding='utf-8')
      data = f.read()
      f.close()
      
      f = open('info.txt', mode='rb')
      data = f.read()
      f.close()
      
    • 读n个字符(字节)【会用到】

      f = open('info.txt', mode='r', encoding='utf-8')
      # 读1个字符
      data = f.read(1)
      f.close()
      
      print(data) # 武
      
      f = open('info.txt', mode='r',encoding='utf-8')
      
      # 读1个字符
      chunk1 = f.read(1)
      chunk2 = f.read(2)
      print(chunk1,chunk2)
      
      f.close()
      
      f = open('info.txt', mode='rb')
      
      # 读1个字节
      data = f.read(3)
      f.close()
      
      print(data, type(data))  # b'\xe6\xad\xa6' <class 'bytes'>
      
      f = open('info.txt', mode='rb')
      
      # 读1个字节
      chunk1 = f.read(3)
      chunk2 = f.read(3)
      chunk3 = f.read(1)
      print(chunk1,chunk2,chunk3)
      
      f.close()
      
  • readline,读一行

    f = open('info.txt', mode='r', encoding='utf-8')
    
    v1 = f.readline()
    print(v1)
    
    v2 = f.readline()
    print(v2)
    
    f.close()
    
    f = open('info.txt', mode='r', encoding='utf-8')
    v1 = f.readline()
    print(v1)
    f.close()
    
    f = open('info.txt', mode='r', encoding='utf-8')
    v2 = f.readline()
    print(v2)
    f.close()
    
  • readlines,读所有行,每行作为列表的一个元素

    f = open('info.txt', mode='rb')
    
    data_list = f.readlines()
    
    f.close()
    
    print(data_list)
    
  • 循环,读大文件(readline加强版)【常见】

    f = open('info.txt', mode='r', encoding='utf-8')
    for line in f:
        print(line.strip())
    f.close()
    
  • write,写

    f = open('info.txt', mode='a',encoding='utf-8')
    f.write("武沛齐")
    f.close()
    
    f = open('info.txt', mode='ab')
    f.write( "武沛齐".encode("utf-8") )
    f.close()
    
  • flush,刷到硬盘

    f = open('info.txt', mode='a',encoding='utf-8')
    
    while True:
        # 不是写到了硬盘,而是写在缓冲区,系统会将缓冲区的内容刷到硬盘。
    	f.write("武沛齐")
        f.flush()
    
    f.close()
    
    file_object = open('files/account.txt', mode='a')
    
    while True:
        user = input("用户名:")
        if user.upper() == "Q":
            break
        pwd = input("密码:")
        data = "{}-{}\n".format(user, pwd)
        file_object.write(data)
        file_object.flush()
    
    file_object.close()
    
  • 移动光标位置(字节)

     在先前的读写操作中,光标的移动都是被动影响,使用f.tell() 查看当前指针的位置
    
    使用f.seek手动指定光标的位置,三种模式 0 1 2 , 
    
    0 模式 f.seek(2,0) 从文件的初始向右移动两个字节
    
    1模式类型必须是b  f.seek(2,1)  在当前的光标的位置向右移动两个字节
    
    2 模式必须是b    f.seek(0,2)   手动定位到文件的末尾。
    
    f = open('info.txt', mode='r+', encoding='utf-8')
    
    # 移动到指定字节的位置
    f.seek(3)
    f.write("武沛齐")
    
    f.close()
    

    注意:在a模式下,调用write在文件中写入内容时,永远只能将内容写入到尾部,不会写到光标的位置。

  • 获取当前光标位置

    f = open('info.txt', mode='r', encoding='utf-8')
    
    p1 = f.tell()
    print(p1)  # 0
    
    f.read(3)  # 读3个字符 3*3=9字节
    
    p2 = f.tell()
    print(p2)  # 9
    
    f.close()
    
    f = open('info.txt', mode='rb')
    
    p1 = f.tell()
    print(p1)  # 0
    
    f.read(3)  # 读3个字节
    
    p2 = f.tell()
    print(p2)  # 3
    
    f.close()
    
  • 证明文件是可读写,可查找的功能

    f = open ("a.txt",mode="at+",encoding="utf-8")
    
        print(f.readable())
        print(f.writable())
        print(f.seekable())
        
    
  • 更改文件内容

    import os
    
    with open('a.txt',mode='a+t',encoding='utf-8') as f1,open('.a.txt.swap',mode='wt',encoding='utf-8') as f2:
        f1.seek(0)
        for line in f1:
            data  = line.replace('111','aaa') # 字符串函数replace 替换的功能
            f2.write(data)
    os.remove('a.txt')
    os.rename('.a.txt.swap','a.txt')
    
  • f.name 返回文件的名称

  • 控制文件指针移动的应用场景

    校验文件的完整性,在文件的各个位置进行取值。
    如果正常情况下,要把文件的所有内容读到内存中,这样增大了服务端的负担,为了解决这个问题,
    把文件的的按比例 分成数段,提取部分数据 ,然后计算,这样既可以保证文件的完整性,又能解决负载的问题。
            f.seek(1000,0)    f.read(10) 只读取10个字节
    
    需要了解的是 文件的任意点,可以使用f.seek(x,x) 达到。
    

1.5 上下文管理

之前对文件进行操作时,每次都要打开和关闭文件,比较繁琐且容易忘记关闭文件。

以后再进行文件操作时,推荐大家使用with上下文管理,它可以自动实现关闭文件。

with open("xxxx.txt", mode='rb') as file_object:
    data = file_object.read()
    print(data)

在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:

with open("xxxx.txt", mode='rb') as f1, open("xxxx.txt", mode='rb') as f2:
    pass

总结

r模式控制指针的位置在文件的开头
w模式控制指针的位置在文件的开头
a模式控制指针的位置在文件的末尾

f.encoding  打印当前文件的编码格式
f.buffer  显示缓存的地址对象
f.fileno 表示系统打开的文件数,类似losf命令的作用
读文件 f.readlines  把每行的数据作为一个元素放到列表中


with open("12345.txt",mode='rt',encoding='utf-8') as f:
data = f.read(2) 
# 只有一种特殊情况 t 模式下 read(n) 
# n可以代表字符 其他全部都是字节




f.seek 延伸
硬盘的修改都是用的新内容覆盖了老的内容
方式1 文件的修改文件的方式 文本编辑器 把文件内容全读到内存中去,然后在内存修改后,再覆盖回硬盘。
缺点 如果源文件过大,很容易把内存加载满,造成系统卡顿
word 等文本编辑器基本上基于方式1 实现。

方式2 文件打开后,逐行修改后,再写入到一个临时文件,然后重命名即可。
缺点 存在了两个一模一样的文件,一旦文件过大,容易占用硬盘,一旦硬盘不足,就不能更改成功

在程序编写过程中,一般采用方式2

with open('123.txt',mode='r+t',encoding='utf-8') as f:
    print(f.writable())
    f.seek(7,0)
    f.write('xx')

with open('123.txt',mode='r+t',encoding='utf-8') as f:
    print(f.writable())
    f.seek(7,0)
    f.write('xx ')

练习题

  1. 通用文件copy工具实现 ,复制文件的工具
source_file  = input("请输入源文件地址:").strip()
des_file  = input("请输入亩地文件地址:").strip()

with open(source_file,mode='r',encoding='utf-8') as source_file_object,open(des_file,mode='w',encoding='utf-8') as des_file_object:
    for line in source_file_object:
        des_file_object.write(line)
        
        

升级版 实现代码简写
src_file = input('输入源文件路径:').strip()
dst_file = input('请输入目标文件路径').strip()

with open(r'%s'%src_file,mode='rb') as old_file, open(r'%s'%dst_file,mode='wb') as new_file:
    new_file.write(old_file.read())
  1. tail -f access.log程序实现

    import time
    
    with open('log.txt', mode='rb') as read_file_log_object:
        read_file_log_object.seek(0, 2)
        # 打开文件,直接定位到文件的最后,此时进入循环状态,
        while True:
            line = read_file_log_object.readline()
            # 如果有数据写入文件,read.line 读到的这行就有数据,赋值给变量line 就能使用len 进行取数据的个数。
            # 读最后一行的内容,如果没有数据写入,就说明line是空数据
            if len(line) == 0:
                # 判断大于0 说明已经在指针右边有数据写入了,
                time.sleep(3)
            print(line.decode('utf-8'),end='')
            # 使用 print 打印出来 。
    
  2. 编写程序实现用户注册后(注册到文件中),可以登录(登录信息来自于文件)

    state = True
    while state:
        key_user = input('请输入要登录的用户名:').strip()
        key_passwd = input('请输入要登录的密码:').strip()
        with open('user_list.txt',mode='r+',encoding='utf-8') as f:
            for line in f:
                line = line.strip()
                line = line.split(":")
                if key_user ==line[0]  and key_passwd == line[1]:
                    print("欢迎")
                    state = False
                    break
                else:
                    print('错误,请重新登陆')
    
    示范1:注册功能
    name = input("your name: ").strip()
    做合法性校验:
    2、如果输入的用户名已经存在也重新输入
    1、如果输入的用户名包含特殊字符^$&...让用户重新输入  ascii转换成字母有对应的数字
    定义一个特殊的字符的集合
    name = set()
    for i in range(65, 91):
        name.add(i)
    for i in range(97, 123):
        name.add(i)
    for i in range(48, 58):
        name.add(i)
    
    while True:
        key_user = input('请输入要注册的用户名:(退出按Q/q)').strip()
        if key_user.upper() == "Q":
            break
        key_passwd = input('请输入要注册的密码:').strip()
    
        with open('user_list.txt', mode='r+', encoding='utf-8') as user_list_file_object:
            for line in user_list_file_object:
                line = line.strip()
                lines = line.split(":")
                if key_user == lines[0]:
                    print('用户名重复,请重新输入')
                    break
            for key in key_user:
                if ord(key) not in name:
                    print('用户名内含有特殊字符,请重新输入')
                    break
    
    
            user_list_file_object.write(f'{key_user}:{key_passwd}\n')
            print('注册完成')
    
    
    
    升级需求1:同一个账号输错三次则退出
    升级需求2:同一个账号输错三次则,该账号则锁定10秒,即便程序被终止,仍然计时
    import os
    import time
    state = True
    count = 0
    while state:
    
        key_name = input('请输入用户名').strip()
        key_passwd = input('请输入密码').strip()
    
        with open('user.txt',mode='r+t') as userlist1:
            for line in userlist1:
                line = line.strip().split(':')
                name, passwd, status_time = line
                # print(name,passwd,status)
    
                if name == key_name and passwd == key_passwd and int(status_time) < int(time.time()):
    
                    longin = True
    
                    break
    
                elif int(status_time) > int(time.time()):
    
                    print(f'用户已被锁定,继续等待{int(status_time) - int(time.time())}')
                    state = False
                    longin=False
                    break
    
    
                else:
                    longin = False
    
            if longin:
                print('登录成功')
                state = False
            else:
                count += 1
    
                if count < 3:
                    print('登录失败')
                else:
    
                    with open('user.txt', mode='rt', encoding='utf-8') as userlist2, open('user.txt.swap', mode='at',
                                                                                         encoding='utf-8') as new_userlist:
                        localtime = int(time.time() + 90)
                        for line in userlist2:
                            name, passwd, times = line.strip().split(':')
                            if name == key_name:
                                new_userlist.write(f'{name}:{passwd}:{localtime}\n')
                                continue
                            new_userlist.write(line)
    
                    os.remove('user.txt')
                    os.rename('user.txt.swap', 'user.txt')
                    errors = input(f'{key_name}错误{count}次,已被锁定')
                    state=False
                    break
    
    
  3. 补充代码:实现下载视频并保存到本地

    import requests
    
    res = requests.get(
        url="https://f.video.weibocdn.com/000pTZJLgx07IQgaH7HW010412066BJV0E030.mp4?label=mp4_720p&template=1280x720.25.0&trans_finger=1f0da16358befad33323e3a1b7f95fc9&media_id=4583105541898354&tp=8x8A3El:YTkl0eM8&us=0&ori=1&bf=2&ot=h&ps=3lckmu&uid=3ZoTIp&ab=3915-g1,966-g1,3370-g1,3601-g0,3601-g0,3601-g0,1493-g0,1192-g0,1191-g0,1258-g0&Expires=1608204895&ssig=NdYpDIEXSS&KID=unistore,video",
        headers={
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
        }
    )
    
    
    # 视频的文件内容
    with open('123.mp4',mode='wb') as mp4:
        mp4.write(res.content)
    
  4. 日志分析,计算某用户223.73.89.192访问次数。日志文件如下:access.log

    49.89.167.91 - - [17/Dec/2020:03:43:50 +0800] "GET /wiki/detail/3/40 HTTP/1.1" 301 0 "-" "Mozilla/5.0(Linux;Android 5.1.1;OPPO A33 Build/LMY47V;wv) AppleWebKit/537.36(KHTML,link Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36 LieBaoFast/4.51.3" "-"
    49.89.167.91 - - [17/Dec/2020:03:44:11 +0800] "GET /wiki/detail/3/40/ HTTP/1.1" 200 8033 "-" "Mozilla/5.0(Linux;Android 5.1.1;OPPO A33 Build/LMY47V;wv) AppleWebKit/537.36(KHTML,link Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36 LieBaoFast/4.51.3" "-"
    203.208.60.66 - - [17/Dec/2020:03:47:58 +0800] "GET /media/uploads/2019/11/17/pic/s1.png HTTP/1.1" 200 710728 "-" "Googlebot-Image/1.0" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /wiki/detail/3/40/ HTTP/1.1" 200 8033 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /static/stark/plugins/font-awesome/css/font-awesome.css HTTP/1.1" 200 37414 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /static/stark/plugins/bootstrap/css/bootstrap.css HTTP/1.1" 200 146010 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /static/web/css/commons.css HTTP/1.1" 200 3674 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /static/mdeditor/editormd/css/editormd.preview.css HTTP/1.1" 200 60230 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /static/stark/js/jquery-3.3.1.min.js HTTP/1.1" 200 86927 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /static/stark/plugins/bootstrap/js/bootstrap.min.js HTTP/1.1" 200 37045 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /static/mdeditor/editormd/lib/marked.min.js HTTP/1.1" 200 19608 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:27 +0800] "GET /static/mdeditor/editormd/lib/prettify.min.js HTTP/1.1" 200 17973 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:27 +0800] "GET /static/mdeditor/editormd/fonts/fontawesome-webfont.woff2?v=4.3.0 HTTP/1.1" 200 56780 "https://pythonav.com/static/mdeditor/editormd/css/editormd.preview.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:27 +0800] "GET /static/mdeditor/editormd/editormd.js HTTP/1.1" 200 163262 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:28 +0800] "GET /static/mdeditor/mdeditor-preview-init.js HTTP/1.1" 200 261 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:29 +0800] "GET /static/stark/plugins/font-awesome/fonts/fontawesome-webfont.woff2?v=4.7.0 HTTP/1.1" 200 77160 "https://pythonav.com/static/stark/plugins/font-awesome/css/font-awesome.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:29 +0800] "GET /media/uploads/2019/02/22/Gobook/_book/ssl2.png HTTP/1.1" 200 203535 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    
    
    
    
    ip = '223.73.89.192'
    count = 0
    with open('user.txt',mode='rt',encoding='utf-8') as log:
    
        for line  in log:
            if ip not in line:
            count+=1
    print(count)
    
    
    ip = '223.73.89.192'
    count = 0
    with open('user.txt',mode='rt',encoding='utf-8') as log:
        for line  in log:
           if  line.startswith(ip):
               count+=1
    print(count)
    
    
    ip = '223.73.89.192'
    count = 0
    with open('user.txt',mode='rt',encoding='utf-8') as log:
        for line  in log:
            if not line.startswith(ip):
               continue
            count+=1
    print(count)
    
    
  5. 日志分析升级,计算所有用户的访问次数。

    ip_user = {}
    num = 1
    with open('user.txt',mode='rt',encoding='utf-8') as log:
        for line  in log:
            line = line.strip().split(' ')
            state = ip_user.get(line[0])
            if state:
                ip_user.update({line[0]:ip_user.get(line[0]),line[0]:ip_user.get(line[0])+1})
            else:
                ip_user.setdefault(line[0],num)
    
    print(ip_user)
    
    
    
    
    iplist = {}
    with open('user.txt',mode='rt',encoding='utf-8') as log:
        for  line in log:
    
            ip_info = line.strip().split(' ')[0]
    
            if ip_info in iplist:
                iplist[ip_info] +=1
            else:
                iplist[ip_info]=1
    print(iplist)
    
  6. 筛选出股票 当前价大于 20 的所有股票数据。

    股票代码,股票名称,当前价,涨跌额,涨跌幅,年初至今,成交量,成交额,换手率,市盈率(TTM),股息率,市值
    SH601778,N晶科,6.29,+1.92,+43.94%,+43.94%,259.66万,1625.52万,0.44%,22.32,-,173.95亿
    SH688566,吉贝尔,52.66,+6.96,+15.23%,+122.29%,1626.58万,8.09亿,42.29%,89.34,-,98.44亿
    SH688268,华特气体,88.80,+11.72,+15.20%,+102.51%,622.60万,5.13亿,22.87%,150.47,-,106.56亿
    SH600734,实达集团,2.60,+0.24,+10.17%,-61.71%,1340.27万,3391.14万,2.58%,亏损,0.00%,16.18亿
    SH900957,凌云B股,0.36,+0.033,+10.09%,-35.25%,119.15万,42.10万,0.65%,44.65,0.00%,1.26亿
    SZ000584,哈工智能,6.01,+0.55,+10.07%,-4.15%,2610.86万,1.53亿,4.36%,199.33,0.26%,36.86亿
    SH600599,熊猫金控,6.78,+0.62,+10.06%,-35.55%,599.64万,3900.23万,3.61%,亏损,0.00%,11.25亿
    SH600520,文一科技,8.21,+0.75,+10.05%,-24.05%,552.34万,4464.69万,3.49%,亏损,0.00%,13.01亿
    SH603682,锦和商业,11.73,+1.07,+10.04%,+48.29%,2746.63万,3.15亿,29.06%,29.62,-,55.42亿
    SZ300831,派瑞股份,12.27,+1.12,+10.04%,+208.29%,25.38万,311.41万,0.32%,60.59,-,39.26亿
    
    
    
    
    with open('user.txt',mode='rt',encoding='utf-8') as gupiao:
        gupiao.readline()
        for line in gupiao:
            date = line.strip().split(",")
            if float(date[2]) <20:
            contune
            print(line)
    
    
  7. 根据要求修改文件的内容,原文件内容如下:ha.conf

    global       
            log 127.0.0.1 local2
            daemon
            maxconn 256
            log 127.0.0.1 local2 info
    defaults
            log global
            mode http
            timeout connect 5000ms
            timeout client 50000ms
            timeout server 50000ms
            option  dontlognull
    
    listen stats :8888
            stats enable
            stats uri       /admin
            stats auth      admin:1234
    
    frontend oldboy.org
            bind 0.0.0.0:80
            option httplog
            option httpclose
            option  forwardfor
            log global
            acl www hdr_reg(host) -i www.luffycity.org
            use_backend www.luffycity.com if www
    
    backend www.luffycity.com
            server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
    ...
    

    请将文件中的 luffycity修改为 pythonav

    # luffycity修改为 pythonav 。
    import os
    
    with open('ha.conf', mode='rt') as file_object, open('ha.conf.swap', mode='wt') as swapfile_object:
        for line in file_object:
            date = line.replace('luffycity', 'pythonav')
            swapfile_object.write(date)
    
    os.remove('ha.conf')
    os.rename('ha.conf.swap', 'ha.conf')
    
    
posted @ 2021-08-11 11:48  mmszxc  阅读(308)  评论(0)    收藏  举报