datetime和subprocess模块
【一】datetime 模块
【1】格式输出我们的日期 / 时间
print(datetime.date(year=2023, month=12, day=20))
print(datetime.time(hour=11, minute=36, second=10))
print(datetime.datetime())
【2】获取本地的时间
print(datetime.date.today()) # 2023-12-20
print(datetime.datetime.today()) # 2023-12-20 11:41:35.468176
print(datetime.datetime.today().year)
print(datetime.datetime.today().month)
print(datetime.datetime.today())
print(datetime.datetime.today().ctime())
【3】打印时间的增减
print(datetime.timedelta(days=7))
【4】today 日期的推迟或者提前
today = datetime.datetime.today()
print(today - t_day)
【5】生日计算器
birthday = datetime.date(1998, 12, 25)
now_date = datetime.date.today()
days = now_date - birthday
print('生日:{}'.format(birthday))
print('今天的日期:{}'.format(now_date))
print('距离生日还有{}天'.format(days))
【二】subprocess模块
import subprocess
"""
1. 使用我们自己的电脑去链接别人的电脑 (socket模块)
"""
res = subprocess.Popen('tasklistaaa', shell=True,
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE
# )
#
print(res) # <subprocess.Popen object at 0x000001ABB1970310>
print(res.stdout.read().decode('gbk')) # tasklist执行之后的正确结果返回
print(res.stderr.read().decode('gbk'))
# 执行 shell 命令 没有返回值
subprocess.call(['ls'])
# 场景 :命令安装模块
try:
import requests
except ModuleNotFoundError:
subprocess.call(['pip', 'install', 'requests'])
标签:datetime,date,模块,print,subprocess,today
From: https://www.cnblogs.com/banchengyanyu/p/17935601.html