Python基础知识学习(五)

1. 时间操作

       实践操作是一个很常用也很重要的技能,它是很多技能实现的基础,比如定时群发邮件、日历等。当我们想要进行实践操作时,需要先导入相应的工具包——time,也就是import time。首先我们要知道,很多编程语言起源于UNIX系统,而UNIX系统认为1970年1月1日0点是时间纪元,所以为人们常说的UNIX时间戳是以1970年1月1日0点为计时起点时间的。以下将举几个时间相关操作的简单例子:

       ①. 获取从1970年到现在的秒数

import time                           # 导入时间工具包
time_line = time.time()               
print(time_line)

       ②. 获取当前(本地)时间

time1 = time.localtime()
print(time1)

       ③. 获取从1970年开始往后指定的秒数所对应的时间

time2 = time.localtime(1530400000)
print(time2)

       ④. 设置自定义时间

time4 = time.strftime('%y-%m-%d %H:%M:%S',time.localtime())
print(time4)

       ⑤. 定时群发

while True :
    # 获取当前时间
    time5 = time.localtime()
    print('检测')
    if time5.tm_year == 2018 and time5.tm_mon == 8 and time5.tm_mday == 2 and time5.tm_hour == 10 :
        print('发送邮件')
        break
    # 线程休眠  sleep 睡觉
    time.sleep(1)

       以上都是在time工具包的基础上实现的简单功能,和时间有关的工具包还有一个,它就是datetime(date是日期的意思,而data是数据的意思),下面还是通过举例说明这个工具包的部分功能:

       ①. 获取今天的时间

import datetime
date1 = datetime.datetime.today()
print(date1)

       ②. 获取现在的时间

date2 = datetime.datetime.now()
print(date2)

       ③. strftime方法

# %y 获取年  %m获取月  %d 获取日
# strftime 不能进行中文编码
date3 = date2.strftime('%yyear%mmonth%dday')
# 但是可以将得到的结果进行转换
print(date3.replace('year','年').
      replace('month','月').replace('day','日'))

       ④. 设置时间间隔

date4 = datetime.timedelta(hours=12 ,minutes= 30)
print(date4)

       ⑤. 从现在往后推迟指定的时间

date5 = datetime.datetime.now() + date4
print(date5)

       ⑥. 只获取当前的时间

# 方法一
date5=  datetime.datetime.today()
date6 = date5.date()
print('-----------------------------')
print(date6)
print('{}年{}月{}日'.format(date6.year ,date6.month ,date6.day))
# 方法二
date7 = date5.time()
print(date7)
print('{}时{}分{}秒'.format(date7.hour ,date7.minute ,date7.second))

       ⑦. 获取当前时间戳

print('当前的时间戳为{}'.format(date5.timestamp()))

2. 日历操作

      和时间操作一样,想要进行日历方面的操作,需要先导入日历包——calendar。下面通过举例来体现简单的日历操作:

      ①. 生成日历

import  calendar
# time1 = time.time()
# date1 = datetime.datetime.today()
# date2 = datetime.datetime.now()
calen = calendar.Calendar()
print(calen)

      ②. 迭代操作

# iterable 可迭代的 for  产品版本迭代
ca1 = calen.iterweekdays()
# 迭代指定的月份   0表示非本月日期
ca1 = calen.itermonthdays(year=2018 ,month=7)
# 迭代指定的月份 获取的元组对象有两个字值

# 值1:是否属于本月 0表示非本月
# 值2:日子对应的星期   0是周一  6是周日
ca1 = calen.itermonthdays2(year=2018 ,month= 7)
# 迭代指定月份的日历 格式为yyyy-mm-dd
ca1 = calen.itermonthdates(year=2018 , month=7)
print(ca1)
for x in ca1 :
    print(x)

      ③. 文本日历

# 获取文本日历
calen = calendar.TextCalendar()
# 给文本日历指定月份
calen.prmonth(theyear=2018,themonth=7)
print(calen)
calen.pryear(theyear=2018)
print(calen)

3. 系统操作

      operation system 操作系统,os模块获取电脑的相关信息,并且有很强大的文件及文件夹操作能力,所以在操作文件或者文件夹的时候,首先要引入os模块。os模块有以下几个常用功能:

      ①. 获取电脑cpu个数

import os
cpuCount = os.cpu_count()
print(cpuCount)

      ②. 获取当前操作系统名称

name = os.name
# nt代表windows操作系统 linux为posix
print('操作系统的名字是:{}'.format(name))

      ③. 获取目标所在(相对)路径

# exists存在 path路径
# 相对路径
result = os.path.exists('1.homework.py')
if result :
    print('存在')
else :
    print('不存在')
print(result)

      注意:平时在windows系统中,我们看到的一个文件的路径格式为:C:\Users\a\Desktop\os测试,但在Python中,我们应该把"\"写作"/",更改之后的路径应写作:C:/Users/a/Desktop/os测试。

猜你喜欢

转载自blog.csdn.net/qq_35866413/article/details/80889049