day6 模块time datetime random os sys json pikle

不写容易忘,感觉好多都陌生了,重新补回来

模块:用一坨代码实现了某个功能的代码集合

time & datetime模块

#_*_coding:utf-8_*_
import time
import datetime
 
print(time.clock()) #返回处理器时间,3.3开始已废弃
print(time.process_time()) #返回处理器时间,3.3开始已废弃
print(time.time()) #返回当前系统时间戳
print(time.ctime()) #输出Tue Jan 26 18:23:48 2016 ,当前系统时间
print(time.ctime(time.time()-86640)) #将时间戳转为字符串格式
print(time.gmtime(time.time()-86640)) #将时间戳转换成struct_time格式
print(time.localtime(time.time()-86640)) #将时间戳转换成struct_time格式,但返回 的本地时间
print(time.mktime(time.localtime())) #与time.localtime()功能相反,将struct_time格式转回成时间戳格式
#time.sleep(4) #sleep
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将struct_time格式转成指定的字符串格式
print(time.strptime("2016-01-28","%Y-%m-%d") ) #将字符串格式转换成struct_time格式
 
#datetime module
 
print(datetime.date.today()) #输出格式 2016-01-26
print(datetime.date.fromtimestamp(time.time()-864400) ) #2016-01-16 将时间戳转成日期格式
current_time = datetime.datetime.now() #
print(current_time) #输出2016-01-26 19:04:30.335935
print(current_time.timetuple()) #返回struct_time格式
 
#datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])
print(current_time.replace(2014,9,12)) #输出2014-09-12 19:06:24.074900,返回当前时间,但指定的值将被替换
 
str_to_date = datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M") #将字符串转换成日期格式
new_date = datetime.datetime.now() + datetime.timedelta(days=10) #比现在加10天
new_date = datetime.datetime.now() + datetime.timedelta(days=-10) #比现在减10天
new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) #比现在减10小时
new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) #比现在+120s
print(new_date)

random 模块

mport random
print random.random()
print random.randint(1,2)
print random.randrange(1,10)

生成随机验证码

import random
checkcode = ‘‘
for i in range(4):
    current = random.randrange(0,4)
    if current != i:
        temp = chr(random.randint(65,90))
    else:
        temp = random.randint(0,9)
    checkcode += str(temp)
print checkcode

OS模块  

提供对操作系统调用的接口

os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname")  改变当前脚本工作目录;相当于shell下cd
os.curdir  返回当前目录: (‘.‘)
os.pardir  获取当前目录的父目录字符串名:(‘..‘)
os.makedirs(‘dirname1/dirname2‘)    可生成多层递归目录
os.removedirs(‘dirname1‘)    若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.mkdir(‘dirname‘)    生成单级目录;相当于shell中mkdir dirname
os.rmdir(‘dirname‘)    删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.listdir(‘dirname‘)    列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove()  删除一个文件
os.rename("oldname","newname")  重命名文件/目录
os.stat(‘path/filename‘)  获取文件/目录信息
os.sep    输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
os.linesep    输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"
os.pathsep    输出用于分割文件路径的字符串
os.name    输出字符串指示当前使用平台。win->‘nt‘; Linux->‘posix‘
os.system("bash command")  运行shell命令,直接显示
os.environ  获取系统环境变量
os.path.abspath(path)  返回path规范化的绝对路径
os.path.split(path)  将path分割成目录和文件名二元组返回
os.path.dirname(path)  返回path的目录。其实就是os.path.split(path)的第一个元素
os.path.basename(path)  返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
os.path.exists(path)  如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path)  如果path是绝对路径,返回True
os.path.isfile(path)  如果path是一个存在的文件,返回True。否则返回False
os.path.isdir(path)  如果path是一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]])  将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
os.path.getatime(path)  返回path所指向的文件或者目录的最后存取时间
os.path.getmtime(path)  返回path所指向的文件或者目录的最后修改时间

sys模块

sys.argv           命令行参数List,第一个元素是程序本身路径
sys.exit(n)        退出程序,正常退出时exit(0)
sys.version        获取Python解释程序的版本信息
sys.maxint         最大的Int值
sys.path           返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform       返回操作系统平台名称
sys.stdout.write(‘please:‘)
val = sys.stdin.readline()[:-1]

json & pickle 模块

用于序列化的两个模块

  • json,用于字符串 和 python数据类型间进行转换
  • pickle,用于python特有的类型 和 python的数据类型间进行转换

Json模块提供了四个功能:dumps、dump、loads、load

pickle模块提供了四个功能:dumps、dump、loads、load

时间: 2024-08-11 12:35:37

day6 模块time datetime random os sys json pikle的相关文章

Python学习第十三天 time datetime random os sysshutil json pickle shelve xml configparser hashlib suprocess logging re

https://www.cnblogs.com/yuanchenqi/articles/5732581.html https://www.cnblogs.com/linhaifeng/articles/6384466.html#_label2 一 time与datetime模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(time.time())”,返回的是fl

模块:time,random,os,sys

时间模块 import time # print(time.time()) #时间戳 # print(time.strftime('%Y-%m-%d %X')) #格式化字符 # print(time.strftime('%Y-%m-%d %H-%M-%S')) # print(time.localtime()) #时间字符串 # print(time.struct_time) #时间戳是计算机能够识别的时间,格式化时间字符串是人能看的时间,元组用来操作时间 # print(time.local

模块( collections , time , random , os , sys)

认识模块: 一条代码 < 语句块 < 代码块(函数, 类) < 模块. collections (克莱克森斯) 1. Counter #用来查看字符出现的次数.s = "upup qwe" print(Counter(s)) 队列: FI FO. 先进先出  栈:  FI LO. 先进后出 2.deque(双向队列) from collections import deque s = deque() s.append("娃哈哈") s.append

8.模块介绍 time &amp;datetime模块 random os sys shutil json &amp; picle shelve xml处理 yaml处理 configparser hashlib subprocess logging模块 re正则表达式

本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configparser hashlib subprocess logging模块 re正则表达式 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.p

time/datetime/os/sys/json/pickle/hashlib/hmac/logging 模块

python常用模块 1.time 模块 # 时间戳:时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. time_stamp=time.time() print(time_stamp,type(time_stamp)) # 1552551519.291029 <class 'float'> # 格式化时间:格式化的时间字符串(format string):格式化时间表示的是普通的字符串格式的时间. format_time=time.strftime("%Y-%

18-[模块]-random&amp;os&amp;sys

1.random模块 程序中有很多地方需要用到随机字符,比如登录网站的随机验证码,通过random模块可以很容易生成随机字符串 # randrange #返回1-3之间的一个随机数,不包含3 # randint #返回1-3之间的一个随机数,包含3 In [32]: random.randrange(1,3) Out[32]: 2 In [33]: random.randrange(1,3) Out[33]: 2 In [34]: random.randrange(1,3) Out[34]: 1

Python全栈开发,Day6 - 模块学习

本章内容 模块介绍 time&datetime random os sys shutil json&pickle shelve xml处理 yaml处理 configarser hashlib subprocess logging 一.模块介绍 模块,用一坨代码实现某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来说,肯呢个需要多个函数才能完成(函数又可以在不同的.py文件中),

python模块time&amp;datetime&amp;json &amp; picle&amp;14.logging等

本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configparser hashlib subprocess logging模块 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个

Python中的time模块与datetime模块用法总结

http://www.jb51.net/article/87721.htm time模块time模块是包含各方面对时间操作的函数. 尽管这些常常有效但不是所有方法在任意平台中有效. time用struct_time表示时间 ? 1 2 3 4 5 6 7 8 import time # time.struct_time(tm_year=2015, tm_mon=4, tm_mday=24,           tm_hour=14, tm_min=17, tm_sec=26,