1. 程式人生 > >day22 函式整理

day22 函式整理

# 1.計算 年月日時分秒 於現在之間差了多少 格式化時間
# 現在
# 某一個年月日時分秒  引數
# import time
# def get_time(old_t,fmt = '%Y-%m-%d %H:%M:%S'):
#     struct_t = time.strptime(old_t,fmt)
#     stmp_old = time.mktime(struct_t)
#     stmp_now = time.time()
#     tmp = stmp_now - stmp_old
#     t = time.gmtime(tmp)
#     return(t.tm_year-1970,
# t.tm_mon - 1, # t.tm_mday -1, # t.tm_hour -0, # t.tm_min -0, # t.tm_sec -0) # div_time = get_time('2018-11-11 17:52:11') # print(div_time)
# 2.4位的字母+數字驗證碼
# import random
# def code(n=4):
#     end = ''
#     for i in range(n):
#         num = str(random.randint(0,9))
# alpha_up = chr(random.randint(65,90)) # alpha_low = chr(random.randint(97,122)) # aim = random.choice([num,alpha_up,alpha_low]) # end += aim # return end # print(code())
# import random#alpha = True生成字母數字驗證碼,alpha=False生成純數字驗證碼
# def code(n=4,alpha = True):
# end = '' # for i in range(n): # aim = str(random.randint(0,9)) # if alpha: # alpha_up = chr(random.randint(65,90)) # alpha_low = chr(random.randint(97,122)) # aim = random.choice([aim,alpha_up,alpha_low]) # end += aim # return end # print(code(n=6,alpha=False)) # print(code())
# 4.檢視某個目錄下的所有檔案(包括子資料夾中的)
# import os
# def list_dir(path):
#     lst = os.listdir(path)
#     for name in lst:
#         full_path = os.path.join(path,name)
#         if os.path.isdir(full_path):
#             list_dir(full_path)
#         else:
#             print(full_path)
# list_dir('E:\s17')
# 5.計算資料夾的總大小
# import os
# def list_dir(path):
#     lst = os.listdir(path)
#     sum_size = 0
#     for name in lst:
#         full_path = os.path.join(path,name)
#         if os.path.isdir(full_path):
#             ret = list_dir(full_path)  # 'E:\s17\day18\day17_作業講解'
#             sum_size += ret
#         else:
#             file_size = os.path.getsize(full_path)
#             sum_size += file_size
#     return sum_size
# ret = list_dir(r'E:\s17')
# print(ret)