• day18


    # DAY18

    ## 复习

    ```python
    import os
    '''
    1.创建一个函数,该函数可以实现查找指定目录下指定文件后缀的文件,
    最终返回存放该目录及子目录下所有满足需求文件绝对路径的列表
    def find_file(abs_path, suffix):
    :param abs_path: 目标目录,绝对路径,str类型数据
    :param suffix: 文件后缀,str类型数据
    :return: 装有所有目录及子目录下满足文件后缀文件的列表
    注:文件夹用abc.txt来命名也是合法的,但是不是文件
    '''
    # def find_file(abs_path, suffix=None, files=[]):
    # '''
    # :param abs_path: 目标目录,绝对路径,str类型数据
    # :param suffix: 文件后缀,str类型数据
    # :return: 装有所有目录及子目录下满足文件后缀文件的列表
    # '''
    # if not os.path.exists(abs_path):
    # return []
    # if os.path.isfile(abs_path):
    # return [abs_path]
    # file_list = os.listdir(abs_path)
    # for file in file_list:
    # file_path = os.path.join(abs_path, file)
    # if os.path.isfile(file_path) and suffix is not None and file.endswith(suffix):
    # files.append(file_path)
    # else:
    # find_file(file_path, suffix, files) # suffix,files显式传递
    # return files
    #
    #
    # res = find_file(r'D:fullstack_s4day18代码part0a', 'py')
    # print(res)


    # def f1():
    # a = 10
    # b = [10]
    # def f2(a=20, b=[]):
    # print(a)
    # print(b)
    # f2(a, b)
    # f1()

    '''
    2.删除目标目录
    def delete_path(abs_path):
    :param abs_path: 要删除的目录路径
    :return: 删除成功与否
    分析:
    1)如果是文件,直接删除
    2)如果是文件夹,就进入,然后删除里面的所有子资源,删除成功后,出来就可以删除文件夹了
    if os.path.isdir(path):
    # 用递归再进入,把path作为目标路径删除
    # 上一步走完了,就可以删除该文件夹了
    3)除了2)的方式,也可以把所有所有文件全部删除,那么就全部是空文件夹,可以一次性删除所有空文件夹
    '''
    # def delete_path(abs_path):
    # '''
    # :param abs_path: 要删除的目录路径
    # :return: 删除成功与否
    # '''
    # if not os.path.exists(abs_path):
    # return False
    # if os.path.isfile(abs_path):
    # os.remove(abs_path)
    # return True
    # file_list = os.listdir(abs_path)
    # for file in file_list:
    # file_path = os.path.join(abs_path, file)
    # if os.path.isfile(file_path):
    # os.remove(file_path)
    # else:
    # delete_path(file_path)
    # os.rmdir(abs_path)
    # return True
    # delete_path(r'D:fullstack_s4day18代码part0ab')

    '''
    3)加密解密文件内容
    定义一个加密函数,调用函数,传入要加密的文件路径,可以将该文件进行加密
    -- 用pickle模块序列化目标文件的内容
    -- 将目标文件转化为文件名.lock文件,如:a.txt加密后,文件名变为 a.txt.lock (创建新文件,pickle写入信息,移除原文件)

    定义一个解密函数,调用函数,传入要解密的文件路径,可以将该文件进行解密
    -- 用pickle模块反序列化目标文件的内容
    -- 再编码为可视的字符,用普通写流,写到a.txt文件,删除a.txt.lock文件
    '''
    import os
    import pickle

    def lock_file(file):
    new_file = file + '.lock'
    with open(file, 'rb') as r, open(new_file, 'wb') as w:
    data = r.read()
    pickle.dump(data, w)
    os.remove(file)

    def unlock_file(file):
    new_file = ''.join(file.rsplit('.', 1)[0])
    with open(file, 'rb') as r, open(new_file, 'wb') as w:
    data = pickle.load(r)
    w.write(data)
    os.remove(file)

    # lock_file(r'D:fullstack_s4day18代码part0a.py')
    unlock_file(r'D:fullstack_s4day18代码part0a.py.lock')
    ```

    ## random:随机数

    ```python
    '''
    (0, 1) 小数:random.random() ***
    [1, 10] 整数:random.randint(1, 10) *****
    [1, 10) 整数:random.randrange(1, 10)
    (1, 10) 小数:random.uniform(1, 10)
    单例集合随机选择1个:random.choice(item) ***
    单例集合随机选择n个(结果是list):random.sample(item, n)
    洗牌单列集合:random.shuffle(item)
    '''
    import random
    # for i in range(10):
    # print(random.random())
    # print(random.randint(1, 10))
    # print(random.randrange(1, 10))
    # print(random.uniform(1, 10))
    # print('-------------------------------------')

    # print(random.choice('abc'))
    # print(random.sample({1, 3, 5, 7, 9}, 3))
    # ls = [1, 2, 3, 4, 5] #只能洗列表
    # random.shuffle(ls)
    # print(ls)

    # 产生指定位数的验证码
    def random_code(count):
    code = ''
    for i in range(count):
    num = random.choice([1, 2, 3])
    if num == 1: # 该位为数字
    code += str(random.randint(0, 9))
    elif num == 2: # 该位为大写字母
    code += chr(random.randint(65, 90))
    else: # 该位 为小写字母
    code += chr(random.randint(97, 122))
    return code

    print(random_code(10))


    def random_code1(count):
    source = 'ABCDEFabcdef0123456789'
    code_list = random.sample(source, count)
    return ''.join(code_list)

    print(random_code1(6))
    ```

    ## shutil:可以操作权限的处理文件模块

    ```python
    import shutil # 对于压缩和解压功能,只能压缩和解压文件夹,文件不行

    # 基于路径的文件复制 不指定就是默认当前执行文件的目录(目录即文件夹)
    shutil.copyfile('a.py', 'b.py')
    # 基于流的文件复制 复制读的文件内容,创建新的文件将其复制过来
    with open('a.py', 'rb') as r, open('c.py', 'wb') as w:
    shutil.copyfileobj(r, w)
    # 文件夹的压缩 (压缩后取的文件夹名,压缩的后缀名,压缩的目标的路径(默认为当前执行文件的路径,如果不指定的话)))
    shutil.make_archive('bbb', 'zip')
    # 文件夹的解压 (解压的文件夹路径,解压文件夹到哪里,解压的方式)(解压谁,解压到哪里,以什么方式解压)
    shutil.unpack_archive('unpack_file', 'unpack_name', 'format')

    ```

    ## shevle:可以用字典

    ```python
    # 将序列化文件操作dump与load进行封装
    s_dic = shelve.open("target_file", writeback=True) # 注:writeback允许序列化的可变类型,可以直接修改值
    # 序列化::存
    s_dic['key1'] = 'value1'
    s_dic['key2'] = 'value2'
    # 反序列化:取
    print(s_dic['key1'])
    # 文件这样的释放
    s_dic.close()

    import shelve

    # 将序列化文件操作dump与load进行封装
    s_dic = shelve.open("target.txt")(查看文件的内容返回字典) # 注:writeback=True操作数据会同步写到文件
    # 序列化::存
    s_dic['key1'] = [1, 2, 3, 4, 5]
    s_dic['key2'] = {'name': 'Bob', 'age': 18}(这些是改变查看文件的内容返回的字典,本意是这样,实际是改变取出到全局的字典,改变的还是全局,文件的不改变,而取值还是取文件的,所以打印的字典还是不变,而writeback就是解决方案)
    s_dic['key3'] = 'abc'
    # 文件释放
    s_dic.close()

    s_dic = shelve.open("target.txt", writeback=True)
    print(s_dic['key1'])
    s_dic['key1'][2] = 30
    print(s_dic['key1'])

    print(s_dic['key2'])
    s_dic['key2']['age'] = 300
    print(s_dic['key2'])

    print(s_dic['key3'])
    s_dic['key3'] = 'def'
    print(s_dic['key3'])
    s_dic.close()


    # from shelve import DbfilenameShelf
    # res = shelve.open("target.txt")
    # print(res, type(res))
    ```

    ## 三流:标准输入输出错误流

    ```python
    import sys
    sys.stdout.write('msg') #只能放入字符串,输出msg,相当于不带换行的Print,这是第一次的打印
    sys.stderr.write('msg') #基于第一次的打印第二次又放入msg,就是变成msgmsg,如果是print的话自带换行符
    msg = sys.stdin.readline() ##在命令行中输入然后回车,就输入啥都行,输入啥打印啥

    # print默认是对sys.stdout.write('msg') + sys.stdout.write(' ')的封装
    # 格式化结束符print:print('msg', end='')

    # 本名:系统标准输入流 | 输出流 | 错误流

    import sys
    # sys.stdout.write('123') #相当于 print('msg', end='') end=''表示不换行
    # sys.stdout.write('123 ') # 相当于 print()
    #
    # print('abc', end=' ')
    # print('abc', end=' ')

    # res = sys.stdin.read(3) 在命令行中输入这代码,然后回车,然后打印如sedfrd,再回车,我再打印sed回车就会跳出,因为规定是只能输入3个字符(不把换行符算上),如果我回车不打印sed,我打印s,再回车打e,再回车就退出了,把换行符算入一个字节
    # print(res)
    res=sys.stdin.read()如果啥也没输就可以不断的输入,一直读

    ```

    ## logging:日志模块

    ```python
    '''
    1) root logging的基本使用:五个级别
    2)root logging的基本配置:logging.basicConfig()
    3)logging模块四个核心:Logger | Filter | Handler | Formater
    4)logging模块的配置与使用
    -- 配置文件:LOGGING_DIC = {}
    -- 加载配置文件:logging.config.dictConfig(LOGGING_DIC) => logging.getLogger('log_name')
    '''


    '''
    bin: 可执行文件 - 项目入口
    conf:配置文件
    core:核心代码
    db:数据库操作
    interface:接口操作
    lib:共有模块(功能)
    log:日志文件
    '''

    ```

  • 相关阅读:
    JS数组去重
    正则表达式验证邮箱手机号等
    js中的事件委托
    c++刷题(6/100)最长上升子序列
    c++刷题(3/100)数独,栈和队列
    在express中HMR(合并express和webpack-dev-server)
    面试整理(3)js事件委托
    面试整理(2)跨域:jsonp与CORS
    面试整理(1):原生ajax
    styled-components真的好吗?
  • 原文地址:https://www.cnblogs.com/huangxuanya/p/10713027.html
Copyright © 2020-2023  润新知