Python入门之时间模块、datetime模块 、difflib文件对比模块、sys模块
作者:互联网
一.时间模块
导入:import time
s = '2019-7-14'
print(time.strptime(s,'%Y-%m-%d'))
s_time = '09:00:00'
print(time.strptime(s_time,'%H:%M:%S'))
1.把元组的时间转换为时间戳
tuple_time = time.localtime()
print(tuple_time)
print(time.mktime(tuple_time))
2.把元组时间转换为字符串时间
print(time.strftime('%m-%d',tuple_time))
print(time.strftime('%Y-%m-%d',tuple_time))
print(time.strftime('%F',tuple_time))
print(time.strftime('%T',tuple_time))
3.把时间戳转换为字符串时间
pwd_time = os.path.getctime('/etc/passwd')
print('pwd_time',pwd_time)
print(time.ctime(pwd_time))
4.把时间戳转换为元组
print(time.localtime(pwd_time))
5.元组转换为时间戳
tuple_time = time.localtime()
print(time.mktime(tuple_time))
二.datetime 模块
导入:import os
from datetime import date
from datetime import datetime
from datetime import timedelta
1.计算几天前后的时间
timedalta
2.计算几个小时前后的时间
now_time = datetime.now()
delta = timedelta(hours = 2)
print(now_time + delta)
print(now_time - delta)
3.计算时间差
now_time = datetime.now()
pwd_time = os.path.getmtime
练习1 需求:
1.获取当前主机时间(操作系统名 主机名 内核版本 硬件架构)
2.获取开机时间与开机时长
3.获取当前登陆用户
import os
import psutil
from datetime import datetime
标签:tuple,Python,difflib,datetime,pwd,模块,time,print,import 来源: https://blog.csdn.net/vanvan_/article/details/95856992