day13常用模块和文件操作

常用模块和文件操作

1.常用模块

1.工作中:

  • os模块 - 文件操作系统(主要提供文件和文件夹相关操作):listdir(目录地址)
    listdir(目录地址) - 返回指定目录下所有的文件的文件名

    import os
    result = os.listdir('./files')
    print(result)
    
  • sys模块 - 系统相关操作,例如exit()

  • json模块 - json数据数据处理 (后面讲)

  • re模块 - 正则表达式相关操作

  • 5)math模块
    ceil(浮点数) - 将浮点数向上取整

    print(int(12.9999))  # 12  直接去掉小数点
    
    print(math.ceil(12.9))   # 13  向上取整
    
    print(math.floor(12.99))   # 12  向下取整
    print(math.floor(-12.99))  # -13  向下取整
    
    print(round(12.1))    # 12
    print(round(12.9))    # 13  四舍五入取整
    print(round(12.4))    # 12
    
    1. *cmath模块 - 复数的数学模块
      complex - 所有复数对应的类型
    a = 10+2j
    print(type(a))
    
    b = 10-2j
    print(a + b)  # (20+0j)
    print(a * b)  # 104 + 0j
    
  • 时间相关模块:time、datatime

  • hashlib - 生成哈希摘要

  • csv - csv文件操作(表格文件操作)

  1. 生活中的使用库和模块

    1)turtle - 画图(哄小孩)
    2)pygame(第三方) - 游戏开发
    3)itchat(第三方) - 微信机器人
    4)smtplib和email - 邮件自动发送和接收
    5)reportlab - pdf文件操作(canvas - 创建pdf水印)
    6) socket - 网络通信

2.hashlib的使用

  1. 什么是hash加密

    1. hash加密的密文(摘要)是不可逆的
      2)相同的数据通过相同的算法生成的密文(摘要)是一样的
      3)不同大小的数据通过相同的算法生成的密文(摘要)的长度是一样的

    hash相关算法:md5和shaXXX

  2. 生成摘要
    1)创建hash对象
    hashlib.算法名()

    hash1 = hashlib.md5()
    

    2)添加原数据
    hash对象.updata(二进制数据)

    hash1.update('123456'.encode())
    

    3)获取摘要
    hash对象

    result = hash1.hexdigest()
    print(result)  # e10adc3949ba59abbe56e057f20f883e
    

3.time模块

  1. 时间戳
    用某一个时间到1970年1月1日0时0分0秒(格林威治时间)的时间差来表示一个时间就是时间戳(单位:秒)

    1)time() - 获取当前时间

t1 = time.time()
print(t1)       # 1612420506.1147552

​ 2)
​ localtime() - 获取当前本地时间,返回结构体时间。 (结构体时间中tm_wday表示星期值,用0~6 表示周一~周日)
​ localtime(时间戳) - 将时间戳对应的时间转换成本地的结构体时间

t2 = time.localtime()
print(t2, t2.tm_mday)

t3 = time.localtime(1612420506.1147552)
print(t3)
  1. datatime模块

    import datetime
    
    • 获取日期

      t4 = datetime.date.today()
      print(t4, t4.year, t4.month)        # 2021-02-04 2021 2
      
    • 获取当前时间

      t5 = datetime.datetime.now()
      print(t5, t5.year, t5.month, t5.day, t5.hour, t5.minute, t5.second)  # 2021-02-04 15:08:05.846125 2021 2 4 15 8 5
      
    • 时间的加减操作

      # 让时间t5减10天
      value = datetime.timedelta(days=10)
      print(t5 - value)  # 2021-01-25 15:08:05.846125
      
      # 让时间t5减24小时
      value = datetime.timedelta(hours=24)
      print(t5 - value)  # 2021-02-03 15:08:05.846125
      
      # 让时间t5加30天零2小时
      value = datetime.timedelta(days=30, hours=2)
      print(t5 + value)   # 2021-03-06 17:10:12.950237
      

4.二进制与字符串之间的转换

  1. 字符串(str)转二进制(bytes)

    方法一:bytes(字符串)
    方法二:字符串.encode()

    # 方法一
    result = bytes('哈哈', encoding='utf-8')
    print(result)      
    # 方法二
    result = '哈哈'.encode()
    print(result, type(result))  # b'\xe5\x93\x88\xe5\x93\x88' <class 'bytes'>
    
  2. 二进制转字符串

    方法一:str(二进制, endcoding=‘utf-8’)
    方法二: 二进制.decode()

    # 方法一
    result = bytes('哈哈', encoding='utf-8')
    s = str(result)
    print(s, type(s))  # b'\xe5\x93\x88\xe5\x93\x88' <class 'str'>
    s = str(result, encoding='utf-8')
    print(s, type(s))  # 哈哈 <class 'str'>
    # 方法二
    result = bytes('哈哈', encoding='utf-8')
    print(result.decode())   # 哈哈
    

4.文件操作

  1. 数据持久化(数据本地化)
    默认情况下程序中所有的数据都是保存在运行内存中的,运行内存中的数据在程序运行结束后会自动销毁只要将数据存储在硬盘中,数据才会一直存在。
    如果想要将程序中产生的数据保存到硬盘中,需要先将数据存储到文件中。(常见的文件:数据库文件、plist文件、Jason文件、TXT文件…)

  2. 文件操作 - 操作文件中的内容

    基本步骤:打开文件 -> 操作文件(读、写) -> 关闭文件

    • 打开文件

      open(file, mode=‘r’, *, encoding=None) - 打开文件并且返回一个文件对象

      1. file - 需要打开的文件的路径(地址)
        a.绝对路径:文件或者文件夹在计算机中的全路径
        例如:E:\yyh\1.txt
        b.绝对路径:写路径的时候只写全路径的一部分,剩下的部分用特殊符号代替
        . - 表示当前目录(当前代码文件所在的目录), 。可以省略
        … - 表示当前目录的上层目录
        … - 表示当前目录的上层目录的上层目录

      2) mode - 文件打开方式 (决定打开文件后能做什么、操作文件的时候对应的数据类型)
      第一组值:决定打开后能做什么,读/写?
      r - 只读
      w - 只写,打开后会清空原文件内容
      a - 只写,打开后不会清空原文件内容
      第二组值:决定操作文件数据的时候对应的数据类型,字符串/二进制?
      t - 文本数据,对应类型是字符串(默认)
      b - 二进制数据,对应的类型是bytes
      两组数据中必须每一组选择一个值:‘r’ == ‘rt’、‘tr’
      格式:‘wb’、‘bw’

      3)encoding - 设置文本文件的编码方式,一般设置成utf-8
      如果是以t的形式打开一个文本文件的时候需要设置encoding。
      打开方式带b绝对不能设置encoding

      # 绝对路径
      open(r'E:\yyh\1.txt')
      
      # 相对路径
      open(r'./files/1.txt')
      
      # r  - 只读
      f = open(r'./files/1.txt', 'r', encoding='utf-8')
      f.read()
      f.write('abc')  # io.UnsupportedOperation: not writable
      
      
      # a - 只写
      f = open(r'./files/1.txt', 'a')
      f.write('abc')
      f.read()    # io.UnsupportedOperation: not readable
      
      # w - 只写,清空
      f = open(r'./files/1.txt', 'w')
      f.read()     # io.UnsupportedOperation: not readable
      f.write('xyz')
      
      # t/b  - 操作数据是字符串
      f = open(r'./files/1.txt', 'rb')
      result = f.read()
      print(result, type(result))  # b'hhh' <class 'bytes'
      
      f = open(r'./files/1.txt', 'ab')
      f.write('abc')
      
      # 注意:打开二进制文件必须带 b
      # r和a、w打开不存在不存在文件   -  r会报错;a、w会新建
      
    • 读写文件

      • # a. 文件对象.read()   -  从读写位置开始读到文件结束 (结束的时候读写位置在文件夹的末尾)
        f = open("./files/1.txt", 'rt', encoding='utf-8')
        result = f.read()
        print(result)
        
        f.seek(0)   # 将读写位置设置到开头
        result = f.read()
        print(result)
        
        # b.文件对象.readline()     -  从读写位置开始到一行的末尾(只有针对以t打开的文本文件)
        f = open("./files/1.txt", 'rt', encoding='utf-8')
        result = f.readline()
        print(result)
        result = f.readline()
        print(result)
        
      • # 文件对象.write(数据)
        f = open(f'./files/1.txt', 'a', encoding='utf-8')
        f.write('\n牛批')
        
  3. 关闭文件

    # 文件对象.close()
    f.close()
    f.write('wawawa')  # ValueError: I/O operation on closed file
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值