前面我们介绍过Python的一些基础语法类容,这期我们继续来学习,有兴趣的可以翻阅我之前的文章。
一:时间处理(一)
首先需要引入时间包
import time
下面我们来写一个 获取从1970年到现在的秒数
import time
time_line = time.time()
print(time_line)
1530876391.4664466
获取当前时间 local 本地
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)
二:时间处理(二)
获取今天的时间
import datetime
date1 = datetime.datetime.today()
print(date1)
2018-07-06 19:53:45.514909
获取现在的时间
date2 = datetime.datetime.now()
print(date2)
2018-07-06 19:53:45.514908
# %y 获取年 %m获取月 %d 获取日
# strftime 不能进行中文编码
date3 = date2.strftime('%yyear%mmonth%dday')
# 但是可以将得到的结果进行转换
print(date3.replace('year','年').
replace('month','月').replace('day','日'))
18年07月06日
设置时间间隔
date4 = datetime.timedelta(hours=12 ,minutes= 30)
print(date4)
12:30:00
从现在往后 推迟指定的时间
date5 = datetime.datetime.now() + date4
print(date5)
只获取当前的日期
date5= datetime.datetime.today()
date6 = date5.date()
print(date6)
print('{}年{}月{}日'.format(date6.year ,date6.month ,date6.day))
2018-07-06
2018年7月6日
只获取当前时间
date7 = date5.time()
print(date7)
print('{}时{}分{}秒'.format(date7.hour ,date7.minute ,date7.second))
19:53:45.514909
19时53分45秒
获取当前时间戳
print('当前的时间戳为{}'.format(date5.timestamp()))
当前的时间戳为1530878025.514909
三:日历
引入日历包 import calendar
import calendar
# time1 = time.time()
# date1 = datetime.datetime.today()
# date2 = datetime.datetime.now()
calen = calendar.Calendar()
print(calen)
获取文本日历
calen = calendar.TextCalendar()
# 给文本日历指定月份
calen.prmonth(theyear=2018,themonth=7)
print(calen)
July 2018
Mo Tu We Th Fr Sa Su
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
获取年文本日历
calen.pryear(theyear=2018)
print(calen)
四:os操作
引入os模块 import os
获取电脑cpu个数
import os
cpuCount = os.cpu_count()
print(cpuCount)
当前文件的绝对路径
result = os.getcwd()
print(result)
# 在计算机当中,获取当前文件路径 用.
# 获取父文件夹路径 用 ..
result = os.path.abspath('.')
print(result)
result = os.path.abspath('..')
print(result)
# 获取指定文件对应的绝对路径
result = os.path.abspath('周二.txt')
print(result)
获取文件路径的某一部分
common公共的
result = os.path.commonpath(['C:/Users/Administrator/Desktop/os测试',
'C:/Users/Administrator/Desktop/同屏',
'C:/Users/Administrator/Desktop/文件夹集合'])
print('路径的公共部分为:{}'.format(result))
路径的公共部分为:C:\Users\Administrator\Desktop
result = os.path.commonpath(['http://www.baidu.com',
'http://www.jd.com',
'http://www.taobao.com'])
print('网址的公共部分为:{}'.format(result))
网址的公共部分为:http:
directory name 获取指定文件所在的文件夹路径
result = os.path.dirname('C:/Users/Administrator/Desktop/os测试/python.txt')
print(result)
getctime get获取
import time
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测试')
isFile 判断是否为文件
os.path.exists()
result = os.path.isfile('C:/Users/Administrator/Desktop/os测试/python.txt')
print('{}'.format(result))
split 分割
result = os.path.split('C:/Users/Administrator/Desktop/os测试/python.txt')
print('{}'.format(result))
('C:/Users/Administrator/Desktop/os测试', 'python.txt')
result =os.path.splitext('C:/Users/Administrator/Desktop/os测试/python.txt')
print('{}'.format(result))
('C:/Users/Administrator/Desktop/os测试/python', '.txt')
文件增删改
值1:修改前的名字 值2:修改后的名字
if os._exists('happy.txt'):
os.rename('happy.txt','葫芦娃.mp3')
删
if os._exists('葫芦娃.mp3'):
os.remove('葫芦娃.mp3')
改变路径到指定的路径下
os.chdir(os.path.pardir)
print('{}'.format(os.getcwd()))
文件读写
open打开 打开指定的文件 如果文件不存在则创建
f = open('os.txt','w',encoding='utf-8')
f.write('Hello World\n')
f.write('你好\n')
f.writelines(['张三\n','李四\n','王五\n'])
f.close()
r :read
f = open('code.txt','r',encoding='utf-8')
content = f.readlines()
print(content)
f.close()
文件内容追加
a:append追加 添加
f = open('new.txt','a',encoding='utf-8')
f.write('谁都不想吃')
f.close()