a、time模块
import time
时间分为三种形式
1、时间戳 (时间秒数的表达形式, 从1970年开始)
print(time.time())
start_time=time.time()
time.sleep(3)
stop_time=time.time()
print(stop_time-start_time)
2、格式化的字符串
print(time.strftime('%Y-%m-%d %H:%M:%S %p'))
print(time.strftime('%Y-%m-%d %X %p'))
3、struct_time对象(结构化时间)
print(time.localtime()) # 上海:东八区
print(time.localtime().tm_year)
print(time.localtime().tm_mday)
print(time.gmtime()) # UTC时区
4、之间转换
print(time.localtime(1111111111).tm_hour)
print(time.gmtime(1111111111).tm_hour)
print(time.mktime(time.localtime()))
print(time.strftime('%Y/%m/%d',time.localtime()))
print(time.strptime('2017/04/08','%Y/%m/%d'))
print(time.asctime(time.localtime()))
print(time.ctime(12312312321))
b、datetime模块
import datetime
print(datetime.datetime.now())
print(datetime.datetime.now() + datetime.timedelta(days=3))
print(datetime.datetime.now() + datetime.timedelta(days=-3))
print(datetime.datetime.now() + datetime.timedelta(hours=3))
print(datetime.datetime.now() + datetime.timedelta(seconds=111))
current_time=datetime.datetime.now()
print(current_time.replace(year=1977))
print(datetime.date.fromtimestamp(1111111111))
print(datetime.date.fromtimestamp(time.time()) )
#时间戳直接转成日期格式 2018-04-08
c、random模块
import random
print(random.random())            #0,1;大于0且小于1之间的小数
print(random.randint(1,3))          #大于等于1且小于等于3之间的整数
print(random.randrange(1,3))       #大于等于1且小于3之间的整数
print(random.choice([1,'a',[1,2,3]]))   #从定义的列表中随机选取
print(random.sample([1,2,3,4,5],3))   #列表中元素任选(3)个数
print(random.uniform(1,3))          #大于1小于3的小数
item=[1,3,5,7,9]
random.shuffle(item)               #打乱item的顺序相当于洗牌
print(item)
生成随机验证码
import random
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(10))
d、打印进度条
def progress(percent,width=50):
    if percent > 1:
        percent=1    #防止显示百分数超过100%
    show_str=('[%%-%ds]' %width) %(int(width*percent) * '#')
    print('\r%s %d%%' %(show_str,int(100*percent)),end='')
import time
recv_size=0
total_size=1111111
while recv_size < total_size:
    time.sleep(0.1)
    recv_size+=8096
    percent=recv_size / total_size
progress(percent)
#
print('[%-30s]'%'#')  # %-50s, -左对齐, 50宽度为50不够空格补
print(('[%%-%ds]'%width)%'#') #第一个百分号取消了第二个百分号的特殊含义
num=30
print('%s%%' %num)    #30%
e、shutil模块
压缩
import shutil
import time
ret = shutil.make_archive(
    "day15_bak_%s" %time.strftime('%Y-%m-%d'),
    'gztar',
    root_dir=r'D:\code\SH_fullstack_s1\day15'
)
f、(zipfile,tarfile) 模块
解压(r)
import tarfile
t=tarfile.open('day15_bak_2018-04-08.tar.gz','r')
t.extractall(r'D:\code\SH_fullstack_s1\day16\解包目录')
t.close()
import zipfile
z=zipfileZipFile('解压文件','r')
z.extractall('解压路径')
z.close
压缩(w)
t=tarfile.open('压缩路劲','w')
t.add('压缩文件')
t.add('压缩文件')
t.close
z=zipfileZipFile('压缩路径','w')
z.write()
z.write()
z.close()
g、sys模块
sys.argv
sys.path
模块内部的一般格式:
import 放在一起
from import放在一起
全局变量的定义
功能函数的定义
if __name__==’__main__’:
    func()
 

(常用)time,datetime,random,shutil(zipfile,tarfile),sys模块的更多相关文章

  1. time/datetime/random/string/os/sys/shutil/zipfile/tarfile - 总结

    time 模块: time.time() #时间戳 time.localtime() #当前时间对象元组 time.localtime(123123) #根据时间戳的时间对象 time.mktime( ...

  2. 模块 - random/string/os/sys/shutil/zipfile/tarfile

    random 模块 方法: >>> random.randint(1,3) #会包含 1 2 3 3 >>> random.randrange(1,3) #会包含 ...

  3. python time、datetime、random、os、sys模块

    一.模块1.定义模块:用来从逻辑上组织Python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test)包:用来从逻辑上组织 ...

  4. 内置模块:time, datetime, random, json, pickle, os, sys, hashlib, collections, re

    1.time模块 import time time.time() # 时间戳 浮点数 time.sleep() # 睡眠 time.gmtime()/time.localtime() #结构化时间 数 ...

  5. 模块之 time datetime random json pickle os sys hashlib collections

    目录 1. time模块 1.1表示时间的几种方式: 1.2格式化字符串的时间格式 1.3不同格式时间的转换 2.datetim模块 3.random模块 4. json模块 4.1dumps.loa ...

  6. 序列化、time、random、hashlib、sys模块

    •很多常用和内置模块,我们只需要掌握他们的用法而暂时不用考虑内部是如何实现的,这些模块大大提升了开发效率 ! 1.json模块与pickle模块 •json 如果你有这样的困扰,当希望把一种数据存到硬 ...

  7. 包+time+datetime+random+hashlibhmac+typing+requests+re模块(day17整理)

    目录 昨日内容 os模块 sys模块 json模块 pickle模块 logging模块 今日内容 包 相对导入 绝对导入 time模块 sleep 时间戳 time 格式化时间 strtime 结构 ...

  8. python之random、time与sys模块

     一.random模块 import random # float型 print(random.random()) #取0-1之间的随机小数 print(random.uniform(n,m)) #取 ...

  9. collections,time,random,os, sys 模块的使用

    主要内容:1. 模块的简单认识2. collections模块3. time时间模块4. random模块5. os模块6. sys模块 一. 模块的简单认识什么是模块. 模块就是我们把装有特定功能的 ...

随机推荐

  1. android 下改变默认的checkbox的 选中 和被选中 图片

    1.   先导入  checked.png 和 unchecked.png 两张图片 2.  在res/drawable下面,添加selector (如 check_state.xml)文件: < ...

  2. shell一些不为人知的技巧

    !$!$是一个特殊的环境变量,它代表了上一个命令的最后一个字符串.如:你可能会这样:$mkdir mydir$mv mydir yourdir$cd yourdir可以改成:$mkdir mydir$ ...

  3. 【CF711D】Directed Roads

    题目大意:给定一个 N 个点,N 条边的无向图,现给每条边定向,求有多少种定向方式使得定向后的有向图中无环. 题解:显然,这是一个外向树森林,定向后存在环的情况只能发生在基环树中环的位置,环分成顺时针 ...

  4. 读入字符串/字符 scanf与getchar/gets区别

    1. 读入字符 scanf/getchar:空格.Tab.回车都可以读入.但要以回车作为结束符. 所以当读入字符时,注意去掉一些干扰输入的字符,如空格和回车 2. 读入字符串 scanf:不能读入空格 ...

  5. gcc/g++

    $gcc -g -Wall -ansi -pedantic main.cpp -lstdc++ -std=c++11 -lpthread -o xmain

  6. Prometheus jvm_exporter监控zookeeper

    Zookeeper Prometheus 监控zookeeper使用jvm_exporter来采集数据,jvm_exporter是一个可以配置抓取和暴露JMX目标的mBeans的收集器. 下载java ...

  7. elasticsearch索引清理

    只是记录两条简单的命令 查看所有的索引文件: curl -XGET http://localhost:9200/_cat/indices?v 删除索引文件以释放空间: curl -XDELETE ht ...

  8. 面向对象【day07】:面向对象引子(一)

    本节内容 概述 面向对象引子 面向过程介绍 一.概述 很对人都不理解编程中的面向对象的概念,那我们先来说说面向对象的引子,由这个引子带领我们更好的理解面向对象的概念. 二.面向对象引子 你现在是一家游 ...

  9. 在IIS上启用Gzip压缩(HTTP压缩)

    一.摘要 本文总结了如何为使用IIS托管的网站启用Gzip压缩, 从而减少网页网络传输大小, 提高用户显示页面的速度. 二.前言. 本文的知识点是从互联网收集整理, 主要来源于中文wiki.  使用Y ...

  10. 转--python -- 收发邮件

    官方 import smtplib from email.mime.text import MIMEText from email.header import Header # 发送邮箱服务器 smt ...