import os print(os.getcwd()) print(os.listdir('./')) print(os.path.abspath('./'))
print(time.time()) # 获取当前时间,返回值为时间戳
print(time.localtime()) print(time.localtime(1627612746.222818)) # time.struct_time(tm_year=2021, tm_mon=7, tm_mday=30, tm_hour=10, tm_min=39, tm_sec=6, tm_wday=4, tm_yday=211, tm_isdst=0)
time.sleep(0)
%Y | %m | %d | %H | %I | %M | %S |
---|---|---|---|---|---|---|
年 | 月 | 日 | 时(24制式) | 时(12制式) | 分 | 秒 |
t1 = time.localtime() result = time.strftime('%z %Y-%m-%d %H:%M:%S', t1) print(result) # +0800 2021-07-30 11:06:08
t2 = '2020-10-20' result = time.strptime(t2, '%Y-%m-%d') print(f'周{result.tm_wday + 1}') # 周2
result = time.mktime(t1) print(result)
t1 = datetime.datetime(2018, 10, 10, 14, 23, 45) print(t1) # 2018-10-10 14:23:45
print(t1.year, t1.minute) # 2018 23
t2 = datetime.datetime.now() print(t2) # 2021-07-30 11:37:39.852374
t3 = t2.timetuple() print(t3) # time.struct_time(tm_year=2021, tm_mon=7, tm_mday=30, tm_hour=11, tm_min=40, tm_sec=8, tm_wday=4, tm_yday=211, tm_isdst=-1)
t4 = t2.strftime('%Y-%m-%d %H:%M:%S') print(t4, type(t4)) # 2021-07-30 11:42:31 <class 'str'>
t5 = datetime.datetime.strptime(t4, '%Y-%m-%d %H:%M:%S') print(t5, type(t5)) # 2021-07-30 11:47:59 <class 'datetime.datetime'>
t1 = '2020-01-01 00:00:00' t2 = datetime.datetime.strptime(t1, '%Y-%m-%d %H:%M:%S') print(t2, type(t2)) # 2020-01-01 00:00:00 <class 'datetime.datetime'> t3 = t2 + datetime.timedelta(days=10) # 增加10天 print(t3) # 2020-01-11 00:00:00 t4 = t3 + datetime.timedelta(weeks=1) # 增加一周时间 print(t4) # 2020-01-18 00:00:00
用于生成数据的hash摘要
hash加密算法主要有:md5 和 shaxx
hash加密的特点:
# hashlib.算法名() hash1 = hashlib.md5()
# hash对象.update(二进制数据) pw = '123456' hash1.update(pw.encode())
result = hash1.hexdigest() print(result) # e10adc3949ba59abbe56e057f20f883e
hash2 = hashlib.sha256() ssh = '123456' hash2.update(ssh.encode()) result = hash2.hexdigest() print(result)
# 字符串转二进制 print(bytes('abc', 'utf-8')) # b'abc' b1 = b'abc' print(b1, type(b1)) # b'abc' <class 'bytes'>
# 二进制转字符串 print(str(b1, 'utf-8')) # abc print(b1.decode()) # abc
open(文件路径, 读写模式, encoding=文件编码方式) - 以指定方式打开指定文件,创建文件对象
文件路径
文件在计算机中的位置信息,以字符串的形式提供值;
绝对路径:文件在计算机里的全路径;
相对路径:. 表示当前代码文件的目录
… 表示当前目录的上层目录
读写模式
设置打开文件后支持的是读操作还是写操作,以及设置操作数据类型(是字符串还是二进制)
第一组值:r(read) - 只读
w(write) - 只写,先清空源文件;
a(append) - 只写,保留源文件,在后面追加;
第二组值:t - 数据类型是字符串(t可以省略)
b - 数据类型是bytes
encoding
# 参数1:路径 licenses = open(r'D:\StudyExperience\Python Data Analysis\01语言基础\Day14 - 常用系统模块\licenses.txt') licenses = open('./licenses.txt') licenses = open('../Day14 - 常用系统模块/licenses.txt') # 参数2:读写模式 # r - 只读 licenses = open('./licenses.txt', 'r') licenses.read() # licenses.write('abc') # io.UnsupportedOperation: not writable # w - 只写;会清空 licenses = open('./licenses.txt', 'w') licenses.write('abc') # licenses.read() # io.UnsupportedOperation: not readable # a - 只写;保留源文件并追加 licenses = open('./licenses.txt', 'a') # licenses.read() # io.UnsupportedOperation: not readable licenses.write('abc') # t - 读写数据的类型是字符串 licenses = open('./licenses.txt', 'rt') result = licenses.read() print(type(result)) # <class 'str'> # b - 读写数据的类型是二进制 licenses = open('./licenses.txt', 'rb') result = licenses.read() print(type(result)) # <class 'bytes'> licenses = open('./licenses.txt', 'ab') # licenses.write('abc') # TypeError: a bytes-like object is required, not 'str' licenses.write(b'abc')
文件对象.close()
# 写一个程序,打印程序运行次数 conf = open('./file/config.txt', 'rt', encoding='utf-8') count = int(conf.read()) count += 1 print(count) conf = open('./file/config.txt', 'w', encoding='utf-8') conf.write(str(count))
# 练习:添加学生,并且在添加文成后显示所有学生信息(只需要学生姓名) while True: stu = open('./file/stu.txt', 'rt', encoding='utf-8') num = str(stu.read()) print(num) in_put = input('请输入姓名:') if in_put == 'q': break stu = open('./file/stu.txt', 'at', encoding='utf-8') stu.write(f' {in_put}')
# 在上题的基础上将学生姓名打印成列表 while True: in_put = input('请输入姓名:') if in_put == 'q': break stu = open('./file/stu.txt', encoding='utf-8') num = eval(stu.read()) num.append(in_put) print(num) stu = open('./file/stu.txt', 'w', encoding='utf-8') stu.write(str(num))