1.time() - 获取当前时间的时间戳
2.localtime() - 获取本地的当前时间,返回的是结构体时间、localtime(时间戳) - 将时间戳转换成本地时间对应的结构体时间
公告结构体时间获取具体的时间信息:时间对象.时间属性名
3.将字符串时间转换成结构体时间:strptime
strptime(字符串时间,时间格式)
时间格式 - 包含时间占位符的字符串
%Y - 年
%m - 月
%d - 日
%H - 时(24小时制)
%I - 时(12小时制)
%M - 分
%S - 秒
%a - 星期缩写
%A - 星期全拼
%b - 月份缩写
%B - 月份全拼
%p - 上午或者下午
示例:
from time import *
t2 = '2002-3-4'
t3 = strptime(t2,'%Y-%m-%d')
print(t3)
# 4.将结构体时间转换成字符串时间
'''
strftime(时间格式,结构体时间)
'''
t5 = localtime()
result = strftime('%Y/%m/%d %I:%S:%p', t5)
# 5.将结构体时间转换成时间戳:mktime(结构体时间)
t5 = localtime()
t6 = mktime(t5)
print(t6)
# 6.睡眠:sleep(秒)
sleep(2)
print('醒了')
datetime模块
示例:
from datetime import datetime, timedelta
# 1.datetime
# 1)获取当前时间
t1 = datetime.now()
print(t1)
t1 = datetime.today()
print(t1)
# 2)通过时间值创建时间对象
t1 = datetime(2020, 8, 10)
print(t1)
# 3)通过时间对象获取时间值
print(t1.year)
print(t1.weekday()) # 0
# 4)时间对象转字符串时间
result = t1.strftime('%Y年%m月%d日')
print(result)
# 5)字符串时间转时间对象
result = datetime.strptime('2020年08月10日', '%Y年%m月%d日')
print(result)
# 6)将时间对象转换成结构体时间
result = t1.timetuple()
print(result)
# 7)获取两个时间的差
t1 = datetime(2020, 3, 5)
t2 = datetime(2021, 5, 10)
r = t2 - t1
print(t2 - t1) # 时间差
# 基于时间差可以单独获取天数和秒数
print(r.days, r.seconds)
# 2.timedelta
# 2012年3月5日的前10天是那一天
t1 = datetime(2012, 3, 5)
print(t1 - timedelta(days=10))
# 2022-4-27五天后是那一天
t2 = datetime(2022,4,27)
print(t2 + timedelta(days= 5))
# 2022-4-27 11:38:20 18小时前
t3 = datetime(2022,4,27,11,38,20)
print(t3 - timedelta(hours=18))
os模块
1.获取当前目录(获取当前py文件所在的文件夹的路径)
import os result = os.getcwd() print(result)2.获取指定目录中所有文件的文件名:os.listdir(文件夹路径)
路径的写法:
1.绝对路径:文件或者文件夹在计算机中的全路径,如果是windows操作系统,绝对路径从盘开始写
2.相对路径:
前提:需要使用的文件或者文件夹必须在当前工程中
a.写路径的时候,用.表示当前目录 (当前目录:获取当前py文件所在的文件夹)
注意:如果相对路径是’./‘开头的’./'可以省略不写
b.写路径的时候,用…表示当前目录的上层目录
示例:
result = os.listdir(r'D:1000phone 428') print(result) result = os.listdir(r'../0414') print(result)创建文件夹
mkdir(文件夹路径) - 在指定位置创建指定文件夹
makedirs(文件夹路径) - 在指定位置创建指定文件夹(递归创建文件夹,如果路径中又多个文件夹不存在,都会自动创建)
示例:
# os.mkdir('./111')
# os.makedirs('./123/1')
path模块中的函数
# 1)获取绝对路径
result = os.path.abspath('.')
print(result)
# 2)获取文件名:basename(文件路径)
result = os.path.basename('./123/1/11.py')
print(result)
# 3)判断文件是否存在:exists(文件路径)
result = os.path.exists('./111/1')
print(result)
# 4)拼接路径:join(路径1,路径2,...)
result = os.path.join(r'D:1000phone 428123','1')
print(result)
# 5)将路径切割成文件夹和文件名两个部分
result =os.path.split(r'./123/1/11.py')
print(result)
# 6)获取文件后缀
result = os.path.splitext(r'./123/1/11.py')
print(result)
# 练习:写一个函数统计指定文件夹中文件和文件夹的数量
def count1(n):
s1 = 0
s2 = 0
list1 = os.listdir(rf'{n}')
for i in list1:
s = os.path.join(rf'{n}',i)
x = os.path.isfile(s)
if x:
s1 += 1
else:
s2 += 1
print(s1,s2)
return s1,s2
count1(r'./123/1')
文件操作
# 1.数据持久化
'''
程序中的数据默认保存在运行内存中,保存在运行内存中的数据在程序运行结束后会自动销毁
如果数据保存在硬盘中,数据会一直存在,直到主动删除或者磁盘损坏
数据持久化 - 指的是以文件为单位将数据保存到硬盘中。(将数据保存到文件中,就是将数据保存到硬盘中)
问题:
a.怎么将程序中的数据保存到文件中? b.怎么将文件中的数据拿到程序中使用
'''
# 2.文件操作
'''
文件操作基本流程:第一步:打开文件 -> 第二步:操作文件(读操作、写操作) -> 第三步:关闭文件
'''
# 1)
open(file,mode'r',*,encoding=None) - 以指定的方式打开指定文件,返回一个文件对象
a.file - 需要打开的文件的路径
b.mode - 文件打开方式(决定打开文件后能干什么;决定操作文件的时候对应的数据的类型)
同时给两组值:
第一组:决定打开文件后能干什么 - 能读还是能写
r - 只读;
w - 只写;打开的时候会清空原文件
a - 只写;打开的时候会保留原文件内容
注意:如果是以读的方式打开一个不存在的文件程序会报错,以写的方式打开不会报错并且会自动创建这个文件
第二组:决定操作数据的类型 - 是字符串(str)还是二进制(bytes)
t - 读写的数据的类型必须是字符串 (不选默认就是t)
b - 读写的数据的类型必须是二进制
赋值方式:每一组值只能选一个,第一组必须选,第二组可以不选,不选就相当于选的t
'rt'、'tr'、'rb'、'br'
注意:二进制文件(图片、视频、音频、zip、pdf等)必须以b的方式打开,文本文件可以以t或者b打开
数据持久化的方法:
第一步:确定需要持久化的数据是什么?
第二步:创建文件,并且确定文件初始内容(分析需要持久化的数据的初始值应该是什么)
第三步:在程序中需要这个数据的时候,从文件中读取这个数据
第四步:当数据发生改变,将最新数据写入文件中
示例:
# 练习:写入一个程序统计当前程序运行的次数
f = open('./111/444.txt','a')
f.write('1')
f.close()
f = open('./111/444.txt','r')
s = f.read()
print(len(s))
# 第一步:程序运行次数
# 第二步:初始次数0 -> 创建文件run_count.txt保存一个0
# 第三步:
f = open('./111/555.txt')
count = int(f.read())
f.close()
count += 1
print(count)
# 第四步:
f = open('./111/555.txt','w')
f.write(str(count))
f.close()
作业1:文件夹整理
import os ,shutil
file_list = os.listdir(r'../0428作业')
print(file_list)
for x in file_list:
s = os.path.splitext(rf'../0428作业/{x}')
s1 = s[-1][1:]
result = os.path.exists(rf'../0428作业/{s1}文件')
if result == False:
os.makedirs(rf'../0428作业/{s1}文件')
shutil.move(rf'../0428作业/{x}', f'../0428作业/{s1}文件')
作业2:小型登录注册
list_password = []
list_keyword = []
def login():
global count
count = 1
if list_password != []:
s = input('请输入账号:')
s1 = input('请输入密码:')
for i in range(len(list_password)):
if s == list_password[i] and s1 == list_keyword[i]:
print('登陆成功')
return
elif s == list_password[i] and s1 != list_password[i]:
print(f'第{count}次')
print('密码错误登陆失败')
count += 1
login()
elif s not in list_password:
print(f'第{count}次')
print('不存在用户名')
count += 1
login()
if count >= 5:
print('失败次数过多退出')
return
else:
print('请先注册!')
regit()
def regit():
s = input('请输入账号1:')
s1 = input('请输入密码1:')
if list_password != []:
if s in list_password:
print('账号重复')
regit()
else:
print('---------------------分割线--------------------------')
list_password.append(s)
list_keyword.append(s1)
print('注册成功')
print(list_password)
print(list_keyword)
func1()
else:
list_password.append(s)
list_keyword.append(s1)
print('注册成功')
print(list_password)
print(list_keyword)
func1()
def func1():
print('---------------------分割线--------------------------')
print(' 1. 登录 ')
print(' 2. 注册 ')
s = input('请输入:')
if int(s) == 1:
login()
elif int(s) == 2:
regit()
else:
print('错误命令')
func1()
func1()



