目录
包的调用
可以看一下day16这里先省略
常用模块
datetime 模块
# 为什么要有datetime模块,datetime模块有什么用
# 时间的加减
#
# import datetime
#
# # 获取当前时间
# now = datetime.datetime.now()
# # print(type(now))
# # now = str(now)
# # print(now.split()[0])
# # print(now.split()[1])
#
# print(now + datetime.timedelta(days=5)) # 默认为天
# print(now - datetime.timedelta(5)) # 默认为天
# print(now + datetime.timedelta(-5)) # 默认为天
#
# # print(now + datetime.timedelta(years=5)) # 无法加年和月
# print(now + datetime.timedelta(hours=5))
# print(now + datetime.timedelta(minutes=65))
# print(now + datetime.timedelta(seconds=65))
#
#
# print(now.replace(year=2012,month=12,day=22,hour=5,minute=13,second=14))
random模块
# import random
# a = random.choice([1,2,'a','h'])
# print(a)
#
# print(random.sample(['a','fg',5,2],3))
# 为什么要有random模块,random模块有什么用
# 随机数
# import random
#
# # 打印(0,1) # 不会取0和1
# print(random.random())
#
# # 打印[n,m]的整数 # 会去n和m
# print(random.randint(1,3))
#
# # 打印(n,m) 的小数
# print(random.uniform(1,3))
#
# # 打印[n,m)的整数
# print(random.randrange(1,3))
# 牢记(从容器中随机选择一个)(******)
# count = 0
# while True:
# res = random.choice([10000,2,1,3,4])
# count += 5000
# if count == 15000:
# print(f'恭喜你获得10000元现金大奖')
# count = 0
# break
# else:
# if res == 10000:
# print('再来一次')
# else:
# print(f'恭喜你获得{res}')
#
# print(f'{"nick"}获得10000元现金大奖')
# print(f'{"tank"}获得1元现金大奖')
# print(f'{"sean"}获得1000元现金大奖')
# # 牢记(从容器中随机选择多个)(******)
# print(random.sample([1,'a',2,'b'],2))
#
# # 牢记(打乱容器)(********)
# lis = [1,2,3,4]
# random.shuffle(lis)
# print(lis)
os模块
# 删除文件
# os.remove(r'D:\上海python全栈9期\Python9期视频\day 17\test.py')
# 重命名文件名
# os.rename(r'D:\上海python全栈9期\Python9期视频\day 17\test.py',r'D:\上海python全栈9期\Python9期视频\day 17\test2.py')
# 打印环境变量
# print(os.environ)
# 牢记(dirname),用在执行文件添加环境变量
# print(os.path.dirname(r'D:\上海python全栈9期\Python9期视频\day 17\03 random模块.py'))
# print(os.path.dirname(__file__))
# # import sys
# # sys.path.append(r'D:\上海python全栈9期\Python9期视频\day 17')
#
# # 判断文件是否存在
# print(os.path.exists(r'D:\上海python全栈9期\Python9期视频\day 17\02 datetime模块1.py'))
#
# # 拼接文件路径,因为不同系统的路径拼接符不一样
# print(os.path.join(r'D:\上海python全栈9期\Python9期视频\day 17','aa.txt'))
#
#
# # (牢记)计算文件大小
# print(os.path.getsize(r'D:\上海python全栈9期\Python9期视频\day 17\01 time模块.py'))
#
# # 打印文件夹下所有文件夹和文件
# print(os.walk(r'D:\上海python全栈9期\Python9期视频\day 17\aaa').__next__())
sys模块
#########sys模块
# 为什么要有sys模块,sys模块有什么用
# 与Python交互
# import sys
#
# # 牢记(当使用Python file.py 参数 ,接收参数
# print(sys.argv)
#
# # 获取解释器的版本
# print(sys.hexversion)
#
# # 牢记(获取当前文件导入模块的路径)
# # print(sys.path) # 调用sys.stdout
#
# # sys.stdout.write('123\n') # c写 print
#
# # print(sys.stdin.read(10)) # c写 input
#
# def print(str,end='\n'):
# str = str+end
# sys.stdout.write('我是新的print\n')
# sys.stdout.write(str)
#
# print('23423423')
# print('23423423')
# print('23423423')
#
# def input():
# print('我是新的input')
# print(sys.stdin.read(10))
# return
#
# input()
pickle模块
# # pickle也是序列化和反序列化,json可以跨平台,只支持dict/list/str/int/float/bool/None
# # pickle支持python中任意数据类型,所以不能跨平台(不同平台的函数一定是不同的),pickle模块序列化过程是以二进制形式转换
#
# def func():
# print('f1')
#
# import pickle
#
# # 了解
# # res = pickle.dumps(func)
# # print(res)
# # pickle.loads(res)()
#
# # pickle由于传输的是二进制,所以应该用b模式
# with open(r'D:\上海python全栈9期\Python9期视频\day 17\m1\test.pkl','wb') as fw:
# pickle.dump(func,fw)
#
# with open(r'D:\上海python全栈9期\Python9期视频\day 17\m1\test.pkl','rb') as fr:
# data = pickle.load(fr)
# data()
标签:pathon,Sep,random,datetime,sys,模块,print,now
From: https://www.cnblogs.com/yyds703/p/18412810