1.时间模块 time
import time
#时间戳转字符串格式
a = time.time()
print(a) #打印时间戳
b = time.localtime(a) #把时间戳转换成时间对象 元组的形式
print(b)
c = time.strftime("%Y-%m-%d %H:%M:%S",b) #格式化时间 把事件对象转化成格式化的字符串
print(c)
#字符串时间转化为时间戳
d = time.strptime("2016-11-14 09:37:26","%Y-%m-%d %H:%M:%S")
print(d)
e = time.mktime(d)
print(e)
#时间加减
import datetime
print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
print(datetime.date.fromtimestamp(time.time()) ) # 时间戳直接转成日期格式 2016-08-19
print(datetime.datetime.now() )
print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
c_time = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) #时间替换
2.random模块 生成随机字符
import random
import string
print( random.randint(1,2)) #包含1和2
print(random.randrange(1,3)) #1和2 会出现,3不会出现
#随机生成验证码或密码
str_source = string.ascii_letters + string.digits
suji = random.sample(str_source,8)
print(suji)
print("".join(suji))
3.shutil 模块 复制,删除,打包压缩
import shutil
#shutil.copy("time模块.py","a") #copy一个文件到另一个文件,包括文件内容和权限
#shutil.copytree(r"C:\Users\Administrator\Documents\Tencent Files\363572453\FileRecv\a","C:\liruixin") #递归的复制目录
#shutil.rmtree("C:\liruixin") #递归的删除目录
#shutil.move() #递归的移动目录
#tmp = shutil.make_archive("C:\svntest","zip",root_dir=r"D:\testsvn") #压缩文件 root_dir为原文件 C盘svntest为压缩后的路径及压缩后的文件名
4.hashlib 模块 可以用来校验文件一致性
import hashlib a = hashlib.md5() #生成一个对象
#a.update(b"abc") #加密
#print(a.hexdigest()) #打印MD5值 #校验一个文件的MD5值
f = open("a")
for i in f:
a.update(i.encode())
print(a.hexdigest())
f.close()
5. logging 模块
import logging
from logging import handlers
#日志输出到屏幕
# logging.warning("waring message")
# logging.critical("server is down")
# logging.error("have error")
# logging.debug("print message")
# logging.info("info")
# #最简单的把日志打印到文件
# logging.basicConfig(filename="test.log",
# format='%(asctime)s %(levelname)s: %(filename)s %(lineno)d %(message)s',
# datefmt='%m/%d/%Y %I:%M:%S',
# level=logging.INFO)
# logging.debug("debug")
# logging.info("info")
# logging.warning("waring")
# logging.error("error")
# logging.critical("critical") #日志多输出
# #create logger
# logger = logging.getLogger("test.log")
# logger.setLevel(logging.DEBUG)
# #create console handler and set level to debug
# ch = logging.StreamHandler()
# ch.setLevel(logging.DEBUG)
# #create file handler and set level to waring
# fh = logging.FileHandler("out.log",encoding="utf-8")
# fh.setLevel(logging.WARNING)
#日志格式
# fh_formatter = logging.Formatter('%(asctime)s %(filename)s:%(lineno)d - %(levelname)s: %(message)s')
# ch_formatter = logging.Formatter('%(asctime)s %(filename)s:%(lineno)d - %(levelname)s: %(message)s')
# #把日志格式加到handler中
# fh.setFormatter(fh_formatter)
# ch.setFormatter(ch_formatter)
#写明要输出的地方
# logger.addHandler(fh)
# logger.addHandler(ch)
#执行打印日志
# logger.debug("I am debug")
# logger.error("I am error") ###日志文件自动截断####
#测试按时间截断
# logger = logging.getLogger("test")
# log_file = "time_log.log"
# fh = handlers.RotatingFileHandler("data.log",maxBytes=2,backupCount=4,encoding="utf-8")
# fh = handlers.TimedRotatingFileHandler(filename=log_file,when="s",interval=3,backupCount=2,encoding="utf-8") #按时间截断
# formatter = logging.Formatter('%(asctime)s %(filename)s :%(lineno)d %(message)s')
# fh.setFormatter(formatter)
# logger.addHandler(fh)
# import time
# logger.error("error1")
# time.sleep(2)
# logger.error("error2")
# time.sleep(6)
# logger.error("error3") # #测试按文件大小截断
# logger = logging.getLogger("test")
# log_file = "data_log.log"
# fh = handlers.RotatingFileHandler("data.log",maxBytes=2,backupCount=4,encoding="utf-8")
# formatter = logging.Formatter('%(asctime)s %(filename)s :%(lineno)d %(message)s')
# fh.setFormatter(formatter)
# logger.addHandler(fh)
# logger.error("error1,error2,error3,error4,")
# logger.warning("waring") ###详细请参考博客 http://www.cnblogs.com/alex3714/articles/5161349.html