内置模块
在python中内置了很多模块,无须额外配置就可使用。
模块和目录的区别:是否有__init__.py
文件,若无则是目录,有则是模块。
datetime
获取当前时间和日期
1 | from datetime import datetime |
datetime
是一个模块,其中包含datetime
类,我们使用的是datetime.now()
中是datetime
这个类。
通过from datetime import datetime
导入的才是datetime
类,若仅导入import datetime
,则需要使用datetime.datetime.now()
方式使用。datetime.now()
返回的是当前日期和时间,类型为datetime
。
格式转换
指定日期和时间
1
2from datetime import datetime
print(datetime(2018, 8, 12, 11, 30)) #输出结果:2018-08-12 11:30:00datetime转换为str
使用datetime.strftime()
实现1
2from datetime import datetime
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) #输出结果:2018-04-23 23:18:10str转换为datetime
使用datetime.strptime()
实现1
2from datetime import datetime
print(datetime.strptime('2018-04-20 18:30:00', '%Y-%m-%d %H:%M:%S')) #输出结果:2018-04-20 18:30:00datetime转换为timestamp
timestamp
也就是常说的时间戳,表示自1970-01-01 00:00:00 UTC+00:00
以来的秒数。
使用timestamp()
实现1
2from datetime import datetime
print(datetime.now().timestamp()) #输出结果:1524497381.170269timestamp转换为datetime
使用fromtimestamp
实现1
2
3from datetime import datetime
print(datetime.fromtimestamp(1524497333.38282)) #输出结果:2018-04-23 23:28:53.382820
print(datetime.utcfromtimestamp(1524497333.38282)) #输出结果:2018-04-23 15:28:53.382820
此处fromtimestamp
转换之后的datetime默认为本地时间,也就是计算机配置的时区对应的时间(本机为UTC+8:00)。
使用utcfromtimestamp
转换之后的datetime为UTC标准时区的时间
- datetime加减
对日期进行向前或者向后的推算,使用timedelta
类1
2
3
4
5
6
7from datetime import datetime
from datetime import timedelta
print(datetime.now()) #输出结果:2018-04-23 23:41:12.694505
print(datetime.now() + timedelta(hours=2)) #输出结果:2018-04-24 01:41:12.694505
print(datetime.now() - timedelta(hours=2)) #输出结果:2018-04-23 21:41:12.694505
print(datetime.now() - timedelta(weeks=1)) #输出结果:2018-04-16 23:41:12.694505
print(datetime.now() + timedelta(days=1)) #输出结果:2018-04-24 23:41:12.694505
timedelta
后面的参数还有minutes
、seconds
、microseconds
。
python中常用的日期时间格式化符号
1 | %y 两位数的年份表示(00-99) |
更多的格式化符号点击查看。
time
在time
模块中,函数gmtime()
、localtime()
和strptime()
以时间元组struct_time
形式返回。
首先介绍时间元组的表示格式
struct_time
时间格式time.struct_time(tm_year=2018, tm_mon=4, tm_mday=24, tm_hour=9, tm_min=3, tm_sec=26, tm_wday=1, tm_yday=114, tm_isdst=0)
索引值|属性|值
-|:-:|:-:
0|tm_year|年(如2015)
1|tm_mon|月,1-12
2|tm_mday|日,1-31
3|tm_hour|时,0-23
4|tm_min|分,0-59
5|tm_sec|秒,0-61
6|tm_wday|星期,0-6(0表示星期一)
7|tm_yday|一年中第几天(1-266)
8|tm_isdst|是否为夏令时。0,1,-1
说明tm_sec
取值范围为0-61是考虑了闰秒和双闰秒的因素。tm_isdst
中,使用夏令时为1,不使用夏令时为0,当不确定是否使用夏令时为-1。
夏令时(DST,Daylight Saving Time)指利用夏天天亮的早,人为的将时间提前一小时,以充分利用光照资源减少照明时间,从而节约照明。
time.time()
返回当前时间的时间戳。
1 | import time |
time.sleep()
线程推迟指定的时间运行,单位为秒。
time.clock()
在unix系统,返回进程时间,用秒表示的浮点数(时间戳)。
在windows系统,第一次调用返回的是进程运行的时间,第二次之后调用均返回自第一次调用以后到现在的运行时间。
1 | import time |
time.localtime([secs)
将一个时间戳转换为当前时区的struct_time
,若未提供secs
参数,返回当前时间的struct_time
。
1 | import time |
time.gmtime([secs])
将一个时间戳转换为UTC时区的struct_time
,若未提供secs
参数,返回当前时间的对应的UTC时区struct_time
。
1 | import time |
time.mktime(t)
将一个struct_time
转换为时间戳。
1 | import time |
time.asctime([t])
把一个表示时间的元组或者struct_time
表示为Sun Jun 20 23:21:05 1993
形式,若没有参数,将会time.localtime()
作为参数传入。
1 | import time |
time.ctime([secs])
把一个时间戳转换为Sun Jun 20 23:21:05 1993
形式,若没有参数或None的时候,默认将time.time()
作为参数。
1 | import time |
time.strftime(format[,t])
把一个表示时间的元组或者struct_time
转换为格式化的时间字符串。如果未指定t(也就是表示时间的元组),传入time.localtime()
。如果元组中的任何一个元素越界,ValueError
错误将会被抛出。
1 | import time |
time.strptime(string[,format])
把一个格式化时间字符串转换为struct_time
。也就是strftime()
的逆操作。
当不指定字符串格式时,format
默认格式为”%a %b %d %H:%M:%S %Y”(下面第二行的格式)。
1 | import time |
- 总结
在time模块中,一共有3种时间表示方式,分别是timstamp
(时间戳)、struct_time
(元组)和格式化字符串
(str)。
- 返回时间戳(timestamp)的方法
time() 无参数
mktime() 参数为struct_time - 返回元组(struct_time)的方法
localtime() 无参数
gmtime() 参数为timestamp
strptime() 参数为str - 返回字符串(str)的方法
asctime() 参数为struct_time
ctime() 参数为timestamp
strftime() 参数为struct_time
更多的关于time
模块内容点击查看。
commands
commands
是linux下特有的模块,用来执行linux命令。cmd代表系统命令。
注意:在python3.X中该模块已移除,使用subprocess
替代。下面均为linux系统中python2.7.5
环境执行结果。
getoutput(cmd)
getoutput(cmd)
返回结果为cmd命令的执行结果(字符串)。
1 | import commands |
getstatusoutput(cmd)
getstatusoutput(cmd)
返回包含2个元素的元组。
第一个元素为命令执行状态(int),如果执行成功返回0,不成功返回非0。
第二个元素为命令执行结果(str)。
1 | import commands |
python中更为常用的是status, result = commands.getstatusoutput("uname -a")
命令,通过一一对一个的方式赋值给status和result。
python内置模块相关官方文档The Python Standard Library点击查看。