常用模块练习

常用模块学习

时间模块

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)
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))

1、随机数骰子

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))

subprocess模块

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'))

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

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)
posted @ 2020-08-16 00:22  Οo白麒麟оΟ  阅读(100)  评论(0编辑  收藏  举报