随笔分类 - python习题
摘要:1. 检索文件夹大小的程序 # 要求执行方式如下 # python3.8 run.py 文件夹 import os import sys def get_file_size(files_path, size=0): for file in os.listdir(files_path): file =
阅读全文
摘要:1. 什么是序列化 序列化是指把内存的数据类型转换成一个特定的格式内容,该格式的内容可用于存储或者传输给其他平台使用 内存中的数据类型——>序列化——>特定的格式(json格式或者pickle格式) 内存中的数据类型<——反序列化<——特定的格式(json格式或者pickle格式) ''' {'aa
阅读全文
摘要:高级的文件、文件夹、压缩包处理模块 1. shutil基本使用 shutil.copyfile(src,dst) # 从源src复制到dst中去。如果当前的dst已存在的话就会被覆盖 shutil.move(src,dst) # 移动文件或重命名 shutil.copymode(src,dst) #
阅读全文
摘要:1. 从文件中取出每一条记录放入列表中,列表的每个元素都是{'name':'egon','sex':'male','age':18,'salary':3000}的形式 with open(r'db.txt',mode='rt', encoding='utf-8') as f: l=[{'name':
阅读全文
摘要:1. 文件内容 标题为:姓名,性别,年纪,薪资 egon male 18 3000 alex male 38 30000 wupeiqi female 28 20000 yuanhao female 28 10000 操作 # 1.从文件中取出每一条记录放入列表中,列表的每个元素都是{'name':
阅读全文
摘要:1. 编写有参函数装饰器 def auth(db_type): def deco(func): def wrapper(*args, **kwargs): name = input('name:').strip() psd = input('password:').strip() if db_typ
阅读全文
摘要:1. 编写函数,(函数执行的时间用time.sleep(n)模拟) import time def count_time(): start = time.time() time.sleep(1) print('我是输出的内容!我要花费零点几毫秒的时间\n') stop = time.time() p
阅读全文
摘要:1. 函数对象优化多分支if的代码练熟 def exit(): print('退出') def login(): print('登录功能') def transfer(): print('转账功能个') def check_balance(): print('余额查询功能') def withdra
阅读全文
摘要:1. 定义域与名称空间解释 input = 333 # 定义全局变量input,赋值333 def func(): # 定义全局函数func input = 444 # 定义局部变量input,赋值444 func() # 调用全局函数func print(input) # 打印全局变量input的
阅读全文
摘要:1. 用户传入修改的文件名,与要修改的内容,执行函数,完成批了修改操作 def func(name, **kwargs): import os if not os.path.exists(r'{}'.format(name)): print('文件路径输入错误') return with open(
阅读全文
摘要:1. 编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改 import os def func(address, old_concent, new_concent): with open(r'{}'.format(address), 'rb')
阅读全文
摘要:1. 编写文件copy工具 ori_file=input('原文件路径:').strip() cop_file=input('新文件路径:').strip() with open(r'{}'.format(ori_file), mode='rt', encoding='utf-8') as f1,\
阅读全文
摘要:1. 有列表['alex',49,[1900,3,18]],分别取出列表中的名字,年龄,出生的年,月,日赋值给不同的变量 l = ['alex', 49, [1900, 3, 18]] name = l[0] age = l[1] age_year = l[2][0] age_month = l[2
阅读全文
摘要:for循环 1. for循环嵌套之打印99乘法表 # ①矩形输出九九乘法表: for i in range(1,10): for j in range(1,10): print(f'{i}X{j}={i*j}',end='') print() # ②左下三角形式九九乘法表: for i in ran
阅读全文
摘要:1. 使用while循环输出1 2 3 4 5 6 8 9 10 count = 0 while count < 10: count += 1 if count == 7: continue print(count) 2. 求1-100的所有数的和 count = 0 i = 0 while cou
阅读全文
摘要:1. 用户输入姓名、年龄、工作、爱好 ,然后打印成以下格式 info of Egon Name : EgonAge : 22Sex : maleJob : Teacher end name = input('Name:') age = input('Age:') sex = input('Sex:'
阅读全文
摘要:嵌套取值操作 1. 请取出第一个学生的第一个爱好 students_info = [['egon', 18, ['play', ]], ['alex', 18, ['play', 'sleep']]] print(students_info[0][2][0]) 2. 针对字典,请取出取公司名 inf
阅读全文

浙公网安备 33010602011771号