Skip to main content
 Web开发网 » 编程语言 » Python语言

几个非常实用的python 模块脚本案例

2021年11月28日6070百度已收录

import timeimport randomimport sysimport os'''1、计算两个格式化时间之间差了多少年月日时分秒'''def time_diff(): new_time = input("请你输入当前的时间,时间格式:%Y-%m-%d %H:%M:%S:") #让用户输入格化时间 old_time = input("请你输入过去的时间,时间格式:%Y-%m-%d %H:%M:%S:") #同上 true_time = time.mktime(time.strptime(new_time,"%Y-%m-%d %H:%M:%S")) #转换成时间戳 now_time = time.mktime(time.strptime(old_time,"%Y-%m-%d %H:%M:%S")) #转换成时间戳 dif_time = now_time - true_time #现在的时间戳减去过去的时间戳得出时间戳的差值 struct_time = time.localtime(dif_time) #将得出来的时间戳转化为结构化时间 #用结构化时间减去时间戳使用的初始值1970 print("过去了%d年%d月%d天%d小时%d分钟%d秒"%(struct_time.tm_year-1970,struct_time.tm_mon-1,struct_time.tm_mday-1,struct_time.tm_hour,struct_time.tm_min,struct_time.tm_sec))'''2、计算当前时间所在月1号的时间戳'''def new_time(): loog_time = input("你输入当前日期至1日,中间相隔多少天:") now = time.time() #当前的时间戳 m1 = now - int(loog_time)*24*60*60 #相隔至每月1号的时间戳 print(time.localtime(m1)) #结构化每月1号时间戳 struct_time = time.mktime(time.localtime(m1)) #计算每月1号的时间戳 print(struct_time) #打印出每月1号的时间戳 print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(struct_time))) #格式化每月1号的时间戳'''3、生成一个6位随机验证码(包含数字和大小写字母)'''def new_random(n = 6): v_code = '' for i in range(n): num = random.randint(0,9) #生成随机数字 str1_num = chr(random.randint(65,90)) #生成大写字母 str2_num = chr(random.randint(97,122)) #生成小写字母 ret = random.choice([num,str1_num,str2_num]) #每次循环从列表中的大小字母和数字中随机选出一个元素 v_code = v_code + str(ret) #每次循环保存上一次结果,一共循环六次(即能模拟6位的随机验证码) print(v_code)'''4、发红包、制定金额和个数随机分配红包金额'''案例一:def new_memory(): #接受转入的参数 raw_user_file_path = sys.argv[1] prize_num = sys.argv[2] #读取参与者邮箱列表文件,这一步需要转入两个参数,第一个是文本文档路径,第二个是一个数字 with open(raw_user_file_path,mode="r",encoding="utf-8") as f1: user_list = f1.readlines() #打印参与者列表,其实参与者列表的打印和抽奖并没有什么关系,抽奖还是会从文件中读取原始数据信息 print('参与奖品编号为:' + str(prize_num) + ' 的抽奖参与者邮箱列表:') print('-' * 27) for i in user_list: print("参与者:" + "****" + i[3:].strip("\n")) print('-' * 27 + "\n" * 2) print("抽奖开始啦") print('-' * 27) count = 0 while count < 3: lucky_user = random.choice(user_list) user_list_input = user_list.remove(lucky_user) if count == 0: print("中奖者为:" + '****' + lucky_user[3:-1]) elif count == 1: print("候选中奖者1为:" + "****" + lucky_user[3:-1]) else: print("候选中奖者2为:" + "****" + lucky_user[3:-1]) count = count + 1 print('-' * 27) print('抽奖结束!' + "\n" *2)案例二:dic = {}lst = ['Keven','John','elxt','cc','Coucs','Zous']def package(cash,people,index): if cash > 0 and people !=1: n = round(random.uniform(0.01,cash - (0.01*people)),2) #n是随机浮点数,round是取小数点后保留2位 dic[lst[index]] = n #这字典用来存放相应的人抢了多少红包金额,然后人数自减少1,金额自减当前抢走的金额index是从人名中顺序取值 print(str(n).ljust(4,"0")) people = people -1 cash = cash - n index = index + 1 package(cash,people,index) #然后递归调用自己把剩余的钱cash,剩余人数people,新位置的人重新传参,如果剩下最后一个人 else: dic[lst[index]] = round(cash,2) print(str(cash).ljust(4,"0"))'''5、分别列出给定目录下所有的文件和文件夹'''def display_file(File_Path,n): #转进去一个目录和数字1 List_Path = os.listdir(File_Path) #列出文件夹内的所有文件或目录的名称,并以列表的形式返回 for D_File in List_Path: #拿到的是文件或目录名 Path_ABS = os.path.join(File_Path,D_File) #拼接文件或目录路径 if os.path.isdir(Path_ABS): #判断是否为目录 print("-"*n,Path_ABS) #为了显示目录或文件名称看的更舒服一点,通过(n*"-")将每一个层级分开 display_file(Path_ABS,n+1) #将一次判断的目录转进去,开始递归整个函数 else: print("-"*n,Path_ABS) #打印文件.'''6、计算某路径下所有文件和文件夹的总大小'''total_size = 0 #定义一个初始值def path_size(filepath): global total_size if not os.path.isdir(filepath): #先判断转进来是不是目录 print("你指定的不是目录") path_list_file = os.listdir(filepath) #扒开你转进来的目录,得的文件是以列表形式返回. for file in path_list_file: #遍历列表里面所有的元素,(这里指的是文件名称或目录名称) path_abs = os.path.join(filepath,file) #拼接目录或文件 if os.path.isfile(path_abs): #判断是否为文件,如果是,就计算大小 total_size = total_size + os.path.getsize(path_abs) else: #如果不是重新递归整个函数 try: #会捕捉异常信息,然后判断打印 path_size(path_abs) except RecursionError: print("递归操作超出边界") return total_sizeif __name__ == '__main__': # time_diff() # new_random(6) # new_time() # new_memory() # package(10, len(lst), 0) # print(dic) # print("手气最佳:", max(dic.items(), key=lambda x: x[1])) # file("F:\python",1) print("一共:%.2fMb"%(path_size("F:\Python-2019")/1024/1024))几个非常实用的python 模块脚本案例  Python脚本 第1张

评论列表暂无评论
发表评论
微信