Python的常用模块(1)

一、Python的常用模块
一、时间模块
1.datetime 时间模块
from datetime import datetime, timedelta

print(datetime.now()) #获取当前时间

print(datetime.now().year) #获取当前年
print(datetime.now().month) #获取当前月
print(datetime.now().day) #获取当前日
print(datetime.now().hour) #获取当前小时
print(datetime.now().minute) #获取当前分钟
print(datetime.now().second) #获取当前秒
print(datetime.now().microsecond) #获取当前毫秒
print(datetime.now().strftime("%Y-%m-%d")) #获取当前年月日

%Y 带世纪部分的十制年份
%m 十进制的月份
%d 十进制表示的每月的第几天
%H 24小时制的小时
%M 十时制表示的分钟数
%c 标准时间类似:04/25/17 14:00:00
strftime 的意思就是 str form time 返回的是一个str类型的时间。一般是一个datetime 类型
与他对应的十strptime 返回一个datetime 类型

now = datetime.now()
now += timedelta(days= -1,weeks= -1,hours= +3) #获取昨天,上周,和小时等时间
print(now)

2.time模块
import time

time.sleep(10) #暂停10秒
print(time.time()) #获取时间戳
print(time.ctime()) #获取标准时间

3.commands模块
commands模块是Linux下独有的模块,用来执行Linux命令的 cmd 代表系统命令
只能在Linux下使用。

import commands

commands 就2个用法。
commands.getoutput("ifconfig") #执行Linux命令,不会返回结果状态码
commands.getstatusoutput("ifconfig") #获取2个结果,一个是状态码,一个是命令执行的结果

4.subprocess模块 和commands模块类型也是调用命令的。支持Windows的dos命令和Linux的命令。

import subprocess
result = subprocess.getoutput("ipconfig")
print(result)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值