time与datetime模块,random模块,os模块,sys模块,configparser模块
一、time与datetime模块
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
import time # 一:时间有三种格式(*****) # 1、时间戳:秒数=>用于时间计算 # start=time.time() # print(start,type(start)) # 2、格式化的字符串=>用于显示给人看 # res=time.strftime("%Y-%m-%d %H:%S:%M %p") # res=time.strftime("%Y-%m-%d %X") # print(res,type(res)) # 3、结构化的时间=>获取时间的某一部分 # res = time.localtime() # res1 = time.gmtime() # print(res,type(res)) # print(res.tm_year) # print(res) # print(res1) # 二:时间转换 # 2.1 时间戳---》格式化的字符串 # struct_time=time.localtime(3333.3) # res=time.strftime("%Y:%m",struct_time) # print(res) # 2.2 格式化的字符串---》时间戳 # struct_time=time.strptime("2017-07-03 11:11:11","%Y-%m-%d %H:%M:%S") # res=time.mktime(struct_time) # print(res) # 三: # print(time.ctime(3333.3)) # print(time.asctime(time.localtime(3333.3))) # 四: # time.sleep(3) #==========================> datetime模块 import datetime # res=datetime.datetime.now() # print(res) # print(datetime.date.fromtimestamp(time.time())) # print(datetime.datetime.fromtimestamp(time.time())) # res=datetime.datetime.now() + datetime.timedelta(days=3) # res=datetime.datetime.now() + datetime.timedelta(days=-3) # print(res) # res=datetime.datetime.now() # print(res) # new_res=res.replace(year=1999,month=9) # print(new_res)
二、random模块
import random # print(random.random()) # print(random.randint(1,3)) # print(random.choice([1,'23',[4,5]])) # print(random.sample([1,'23',[4,5]],2)) # print(random.uniform(1,3)) # item=[1,3,5,7,9] # random.shuffle(item) # print(item) # 随机验证码 # print(chr(65)) # print(chr(90)) def make_code(n=5): res = '' for i in range(n): s1 = str(random.randint(0, 9)) s2 = chr(random.randint(65, 90)) res += random.choice([s1, s2]) return res print(make_code(4))
三、os模块
# import os # print(os.listdir(".")) # import os # cmd=input(r""" # Microsoft Windows [版本 10.0.17763.1339] # (c) 2018 Microsoft Corporation。保留所有权利。 # # %s=====> """ %os.getcwd()) # res=os.system(cmd) # print('='*100) # print(res) import os # print(__file__) # print(os.path.abspath(__file__)) # res=os.path.split("D:/python全栈15期/day21/03 os模块.py") # print(res) # print(os.path.basename("D:/python全栈15期/day21/03 os模块.py")) # print(os.path.dirname("D:/python全栈15期/day21/03 os模块.py")) # print(os.path.exists("D:/python全栈15期/day21/")) # print(os.path.isabs("D:/python全栈15期/day21/")) # print(os.path.isabs("/python全栈15期/day21/")) # print(os.path.isabs("python全栈15期/day21/")) # print(os.path.join("a",'b','D:\c','d.txt')) # print(os.path.dirname(os.path.dirname(__file__))) # res=os.path.join( # __file__, # "..", # ".." # ) # # print(os.path.normpath(res)) print(os.path.getsize(__file__))
四、sys模块
import sys # sys.path # print(sys.argv) # src_file = input("源文件路径:").strip() # dst_file = input("目标文件路径:").strip() # src_file = sys.argv[1] # dst_file = sys.argv[2] # # with open(r"%s" %src_file,mode='rb') as f1,open(r"%s" %dst_file,mode='wb') as f2: # for line in f1: # f2.write(line)
打印进度条
# import time # print("[# ]",end='') # time.sleep(0.3) # print("\r[## ]",end='') # time.sleep(0.3) # print("\r[### ]",end='') # time.sleep(0.3) # print("\r[#### ]",end='') # print("[%-15s]" % "###",) # print("[%-15s]" % "####",) import time def progress(percent): if percent > 1: percent=1 print("\r[%-50s] %d%%" % ("#"*int(50*percent),int(100 * percent)),end='') total_size = 1025 recv_size = 0 while recv_size < total_size: # 每次下载1024 time.sleep(0.3) recv_size+=1024 percent = recv_size / total_size progress(percent)
五、configparser模块
import subprocess # pwd ; sasdfasdf ;echo 123 obj = subprocess.Popen("diasfdafr", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) res1=obj.stdout.read() res2=obj.stderr.read() # print(res1.decode('gbk')) print(res2.decode('gbk'))
更多精彩