zoukankan      html  css  js  c++  java
  • python学习之day6,常用标准模块

    1.时间模块 time

     1 import  time
     2 #时间戳转字符串格式
     3 a = time.time()
     4 print(a)  #打印时间戳
     5 b = time.localtime(a)   #把时间戳转换成时间对象  元组的形式
     6 print(b)
     7 c = time.strftime("%Y-%m-%d %H:%M:%S",b)    #格式化时间  把事件对象转化成格式化的字符串
     8 print(c)
     9 #字符串时间转化为时间戳
    10 d = time.strptime("2016-11-14 09:37:26","%Y-%m-%d %H:%M:%S")
    11 print(d)
    12 e = time.mktime(d)
    13 print(e)
    14 #时间加减
    15 import datetime
    16 print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
    17 print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2016-08-19
    18 print(datetime.datetime.now() )
    19 print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
    20 print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
    21 print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
    22 print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
    23 c_time  = datetime.datetime.now()
    24 print(c_time.replace(minute=3,hour=2)) #时间替换

    2.random模块  生成随机字符

    1 import  random
    2 import string
    3 print( random.randint(1,2))  #包含1和2
    4 print(random.randrange(1,3))  #1和2 会出现,3不会出现
    5 #随机生成验证码或密码
    6 str_source = string.ascii_letters + string.digits
    7 suji = random.sample(str_source,8)
    8 print(suji)
    9 print("".join(suji))

    3.shutil 模块     复制,删除,打包压缩

    1 import shutil
    2 #shutil.copy("time模块.py","a") #copy一个文件到另一个文件,包括文件内容和权限
    3 #shutil.copytree(r"C:UsersAdministratorDocumentsTencent Files363572453FileRecva","C:liruixin")  #递归的复制目录
    4 #shutil.rmtree("C:liruixin") #递归的删除目录
    5 #shutil.move()  #递归的移动目录
    6 #tmp = shutil.make_archive("C:svntest","zip",root_dir=r"D:	estsvn")  #压缩文件 root_dir为原文件   C盘svntest为压缩后的路径及压缩后的文件名

    4.hashlib 模块   可以用来校验文件一致性

     1 import hashlib
     2 
     3 a = hashlib.md5() #生成一个对象
     4 #a.update(b"abc")  #加密
     5 #print(a.hexdigest())  #打印MD5值
     6 
     7 #校验一个文件的MD5值
     8 f = open("a")
     9 for i in f:
    10     a.update(i.encode())
    11 print(a.hexdigest())
    12 f.close()

     5. logging 模块

     1 import logging
     2 from logging import  handlers
     3 #日志输出到屏幕
     4 # logging.warning("waring message")
     5 # logging.critical("server is down")
     6 # logging.error("have error")
     7 # logging.debug("print message")
     8 # logging.info("info")
     9 # #最简单的把日志打印到文件
    10 # logging.basicConfig(filename="test.log",
    11 #                     format='%(asctime)s  %(levelname)s: %(filename)s %(lineno)d  %(message)s',
    12 #                     datefmt='%m/%d/%Y %I:%M:%S',
    13 #                     level=logging.INFO)
    14 # logging.debug("debug")
    15 # logging.info("info")
    16 # logging.warning("waring")
    17 # logging.error("error")
    18 # logging.critical("critical")
    19 
    20 
    21 
    22 #日志多输出
    23 # #create logger
    24 # logger = logging.getLogger("test.log")
    25 # logger.setLevel(logging.DEBUG)
    26 # #create console handler and set level to debug
    27 # ch = logging.StreamHandler()
    28 # ch.setLevel(logging.DEBUG)
    29 # #create file handler and set level to waring
    30 # fh = logging.FileHandler("out.log",encoding="utf-8")
    31 # fh.setLevel(logging.WARNING)
    32 #日志格式
    33 # fh_formatter = logging.Formatter('%(asctime)s  %(filename)s:%(lineno)d   - %(levelname)s: %(message)s')
    34 # ch_formatter = logging.Formatter('%(asctime)s  %(filename)s:%(lineno)d   - %(levelname)s: %(message)s')
    35 # #把日志格式加到handler中
    36 # fh.setFormatter(fh_formatter)
    37 # ch.setFormatter(ch_formatter)
    38 #写明要输出的地方
    39 # logger.addHandler(fh)
    40 # logger.addHandler(ch)
    41 #执行打印日志
    42 # logger.debug("I am debug")
    43 # logger.error("I am error")
    44 
    45 ###日志文件自动截断####
    46 #测试按时间截断
    47 # logger = logging.getLogger("test")
    48 # log_file = "time_log.log"
    49 # fh = handlers.RotatingFileHandler("data.log",maxBytes=2,backupCount=4,encoding="utf-8")
    50 # fh = handlers.TimedRotatingFileHandler(filename=log_file,when="s",interval=3,backupCount=2,encoding="utf-8") #按时间截断
    51 # formatter = logging.Formatter('%(asctime)s %(filename)s :%(lineno)d %(message)s')
    52 # fh.setFormatter(formatter)
    53 # logger.addHandler(fh)
    54 # import time
    55 # logger.error("error1")
    56 # time.sleep(2)
    57 # logger.error("error2")
    58 # time.sleep(6)
    59 # logger.error("error3")
    60 
    61 # #测试按文件大小截断
    62 # logger = logging.getLogger("test")
    63 # log_file = "data_log.log"
    64 # fh = handlers.RotatingFileHandler("data.log",maxBytes=2,backupCount=4,encoding="utf-8")
    65 # formatter = logging.Formatter('%(asctime)s %(filename)s :%(lineno)d %(message)s')
    66 # fh.setFormatter(formatter)
    67 # logger.addHandler(fh)
    68 # logger.error("error1,error2,error3,error4,")
    69 # logger.warning("waring")
    70 
    71 ###详细请参考博客 http://www.cnblogs.com/alex3714/articles/5161349.html
  • 相关阅读:
    解读《TCP/IP详解》(卷1):05章:RARP(逆地址解析协议)
    Android中利用“反射”动态加载R文件中的资源
    “骆驼”怎么写
    eclipse无法访问genymotion模拟器下/data/data文件解决方案
    解读《TCP/IP详解》(卷1):03章:IP(网际协议)
    解读《TCP/IP详解》(卷1):02章:链路层
    hdu1039 java正则表达式解法
    hdu1027 又是next_permutaiton
    hdu1261 java水过高精度排列组合。。
    hdu1716 STL next_permutation函数的使用
  • 原文地址:https://www.cnblogs.com/liruixin/p/6061612.html
Copyright © 2011-2022 走看看