1.时间处理
#首先引入时间包
import time
time_line = time.time()
print(time_line)
time1 = time.localtime()
#得到一个结构体
print(time1)
time2=time.strftime('%y-%m-%d %H:%M:%S',time.localtime())
print(time2)
#引入日期包
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)
2.日历
#引入日历包
import calendar
calen = calendar.Calendar()
print(calen)
#iterable可迭代的
cal = calen.itermweekdays()
cal = calen.itermonthdays(year = 2018, month = 7)
#迭代指定的月份 获取的元组对象有两个值
#值一:是否是本月 0表示非本月
#值二:日子对应的星期 0周日
cal = calen.itermonthdays2(year = 2018 , month=7)
# 0表示非本月日期、
cal = calen.itermonthdates(year = 2018,month =7)
#获取迭代指定的日期
print(cal)
for x in cal:
print(x)
3.os操作
import os
#获取电脑核数
cpu_count=os.cpu_count()
print(cpu_count)
#获取操作系统的名字:nt代表windows操作系统
linux为POSIX
name = os.name
print('操作系统的名称为:{}'.format(name))
#exitst 存在 path :路径
result = os.path.exists('1.作业.py')
if result:
print('存在')
else:
print('不存在')
#获取当前的绝对路径
result = os.getcwd()
print(result)
result = os.path.abspath('.')
print(result)
#获取父文件路径
result = os.path.abspath('..')
print(result)
#获取指定文件对应的绝对路径
result = os.path.abspath('日历.py')
print(result)
#获取文件夹信息 创建日期 修改日期 访问日期
#access:访问 getatime 创建时间:getctime
修改时间 :getmtime
result = os.path.getctime('C:/Users/Administrator/Desktop/os测试')
print('{}'.format(time.localtime(result)))
result = os.path.getatime('C:/Users/Administrator/Desktop/os测试')
print('文件的访问日期是:{}'.format(time.localtime(result)))
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))
#判断是否为文件
result = os.path.isfile('C:/Users/Administrator/Desktop/os测试/python.txt')
print('{}'.format(result))
#文件夹增删改操作
#值一修改前的名字
#值二修改后的名字
if os._exists('周二.TXT'):
os.rename('周二.TXT','happy.txt')
if os._exists('happy.txt'):
os.remove('happy.txt')
作业:
1.随意输入一个输入,如果是1 创建文件夹名称为test_one,如果是2则删除
import os
result = input('请输入你的数字')
result=int(result)
if result==1:
if os.path.exists(test_one):
pass
else:
os.mkdir('test_one')
elif result==2:
if os.path.exists('test_one'):
os.removedirs(test_one'')
eise:
print('请输入正确的数字')
2.创建一个文件 ,名字为code.txt,在里面存放10000个10位随机验证码
import random
str = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789'
f = open('code.txt','r',encoding='utf-8')
for x in range(10000):
result=''
for y in range(10):
char_str = random.choice(str)
result += char_str
f.write(result+'\n')
f.close()
3.获取练习2中的文件,然后判断每个验证码中每个数字出现次数的总数
date ={}
for x in range(10):
date[str(x)]=0
print(date)
f = open('code1.txt','r',encoding='utf-8')
for line in f.readlines():
for char in line:
for key in date:
if char == key
date[key]+=1
print(date)