Python基础知识——时间处理、日历、os操作

2022-09-17 13:28:46

继昨天的基础小知识之后(https://mp.csdn.net/postedit/80864311),今天介绍一些有关于时间处理、日历以及os的操作。

一、时间处理

      1.时间包的导入、本地时间的获取以及自定义时间的设置

# 引入时间包
import time
time_line = time.time()

# 获取从1970年到现在的秒数
# 1970年Linux操作系统出现
print(time_line)
#time.struct_time  struct 结构体
# 0 周一  6 周日  local本地
time1 = time.localtime()
print(time1)

# 获取从1970年开始往后指定的秒数所对应的时间
time2 = time.localtime(1530497140)
print(time2)
# 设置自定义时间 2018-07-02  12:12:12
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 == 7 and time5.tm_mday == 2 and time5.tm_hour == 10 :
        print('发送邮件')
        break
    # 线程休眠 sleep 休眠
    time.sleep(1)

     2.获取时间、时间间隔设置、获取时间戳

import datetime
# 获取今天的时间
date1 = datetime.datetime.today()
print(date1)

# 获取现在的时间
date2 = datetime.datetime.now()
print(date2)

# %y 获取年  %m 获取月  %d 获取日
# strftime 不能进行中文编码
date3 = date2.strftime('%yyear%mmonth%dday')
print(date3.replace('year','年').replace('month','月').replace('day','日'))
# 设置时间间隔
date4 = datetime.timedelta(days=1 ,hours=12)
print(date4)
# 从现在往后 推迟指定时间
date5 = datetime.datetime.now() + date4
print(date5)
# 只获取当前日期
date6 = datetime.datetime.today()
date6 = date5.date()
print(date6)
print('{}年{}月{}日'.format(date6.year,date6.month,date6.day))

# 只获取当前时间
date7 = date5.time()
print(date7)
# print('{}时{}分{}秒'.format(date7.hour,date6.min,date6.seconds))
# 获取当前时间戳
print('当前的时间戳为{}'.format(date5.timestamp()))

二、日历

      1.引入日历包

import calendar
calen = calendar.Calendar()
print(calen)
      2.迭代指定的月份,迭代指定的月份获取的元组对象有两个值,值1是属于本月,值2是日子对应的星期,0是周一,6是周日。
# 迭代指定的月份  0表示非本月日期
ca1 = calen.itermonthdays(year=2018,month=7)
# 迭代指定的月份 获取的元组对象有两个值
# 值1 :是否属于本月  值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)

calen.pryear(theyear=2018)
print(calen)
三、os操作

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

import os
# 获取电脑cpu个数(核数)
cpuCount = os.cpu_count()
print(cpuCount)
name = os.name
# nt 代表windows操作系统 linux为posix
print('操作系统的名字是:{}'.format(name))

   2.文件路径

     用.exists判断路径是否存在,当前文件的绝对路径用.getcwd(),还可以用.abspath('.')/.abspath('..'),分别为当前文件的路径和父路径。获取文件的某一部分路径用.basename(),获取指定文件所在的路径用.dirname()。

# 判断路径是否存在
result = os.path.exists('C:/Users/Administrator/Desktop/os测试/python.txt')
print(result)
# 当前文件的绝对路径
result = os.getcwd()
print(result)

# absolute 绝对的
# 在计算机中,获取当前文件路径用 .   获取父文件路径用..
result = os.path.abspath('.')
print(result)
result = os.path.abspath('..')
print(result)
# 获取文件路径的某一部分
result = os.path.basename('C:/Users/Administrator/Desktop/os测试')
print('路径的basename:{}'.format(result))

# 注意以斜杠分割 将路径分成几部分 找到公共的这一部分
result = os.path.commonpath(['http://www.baidu.com','http://www.jd.com','http://www.taobao.com'])
print('网址的公共部分为:{}'.format(result))
# directory name 获取指定文件所在的文件路径
result = os.path.dirname('C:/Users/Administrator/Desktop/os测试/python.txt')
print(result)
# 获取文件夹信息 文件夹信息包括创建日期 修改日期 访问日期
import  time
# c 文档是:change 实际是:create    getctime 获取
result = os.path.getctime('C:/Users/Administrator/Desktop/os测试')
print('文件创建的日期为:{}'.format(time.localtime(result)))

# a:access 访问
result = os.path.getatime('C:/Users/Administrator/Desktop/os测试')
print('文件的访问日期是:{}'.format(time.localtime(result)))

# m : modify 修改
result = os.path.getmtime('C:/Users/Administrator/Desktop/os测试')
print('文件的修改日期是:{}'.format(time.localtime(result)))
# size 尺寸,大小
result = os.path.getsize('C:/Users/Administrator/Desktop/os测试')
# 获取的为字节大小
print('文件的大小为:{}'.format(result / 1024))
# isFile 判断目标对象是否为一个文件
# os.path.exists()
result = os.path.isfile('C:/Users/Administrator/Desktop/os测试')
print('{}'.format(result))
# 文件分割
# 分割路径 两部分  1.除最后路径外的最后路径  2.最后路径
result = os.path.split('C:/Users/Administrator/Desktop/os测试/python.txt')
print('{}'.format(result))

#  1.全部路径  2.文件后缀
result = os.path.splitext('C:/Users/Administrator/Desktop/os测试/python.txt')
print('{}'.format(result))
# 文件夹增删改操作-------------------
# 值1 :修改前的名字   值2 :修改后的名字
if os._exists('周二.txt'):
    os.rename('周二.txt', 'happy.txt')
if os._exists('happy.txt'):
    os.remove('happy.txt')
# make directory 创建文件夹
# os.mkdir('test')
  • 作者:莫悔
  • 原文链接:https://blog.csdn.net/ljj950408/article/details/80889001
    更新时间:2022-09-17 13:28:46