1、时间和随机数
# -*- encoding=utf-8 -*-
import datetime
import random
import time
import uuid
if __name__ == '__main__':
# 获取当前时间
now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print('现在时间:{}'.format(now)) # 2020-07-23 10:14:19
# 格式化时间
date_time = datetime.datetime.strptime('2020-07-23', '%Y-%m-%d')
print('格式化时间:{}'.format(date_time))
# UUID
u = str(uuid.uuid1())
print('UUID:{}'.format(u)) # 756e2f8f-cc8a-11ea-8773-d850e6d359bc
# 随机数
r = random.randint(0, 10) # 包括0和10
print('随机数:{}'.format(r))
# 当前时间随机加0-120秒
random_now = (datetime.datetime.now() + datetime.timedelta(seconds=random.randint(0, 120))).strftime(
"%Y-%m-%d %H:%M:%S")
print('当前时间随机加0-120秒:{}'.format(random_now))
2、获取某个文件夹下所有文件
import os
def traverse_folder(father_folder):
father_folder = os.path.abspath(father_folder)
all_files = []
all_dirs = []
if os.path.isdir(father_folder):
for root, dirs, files in os.walk(father_folder):
for one_file in files:
all_files.append(os.path.join(root, one_file)) # 所有文件
for one_dir in dirs:
all_dirs.append(os.path.join(root, one_dir)) # 所有文件夹
else:
print('未找到文件夹:{}'.format(father_folder))
return all_dirs, all_files
if __name__ == '__main__':
a, b = traverse_folder('A')
print(a)
print('=============')
print(b)