标准库:
安装python解释器后,直接自带的os,sys,json,csv,time,datetime,hashlib
os库
代码:
import os print("当前路径",os.getcwd()) for xiangmu in os.listdir(path=os.getcwd()): print(xiangmu) print("获取文件和目录信息:",os.stat("D://code//testDev//复习库")) # 获取当前路径 print(os.path.dirname(__file__)) # 当前路径的上一级路径 print(os.path.dirname(os.path.dirname(__file__))) on1=(os.path.dirname(os.path.dirname(__file__))) filePath=(os.path.join(on1, "data1", "enroll.txt")) with open(filePath,"r",encoding="utf-8") as f: print(f.read()) print('获取操作系统:',os.name) print('环境变量',os.environ) print(os.path.exists(filePath))#判断filepath是否存在
输出结果:
json库
代码:
# 序列化与反序列化 import json # 列表的序列化与反序列化 list1=[1,2,3,4,"a","c"] list1_str=json.dumps(list1) print(list1_str) print(type(list1_str)) print(type(list1)) str_list1=json.loads((list1_str)) print(str_list1) print(type(str_list1)) list2=[4,5,8,6,'a','c'] list2_str=json.dumps(list2) print(list2_str) print(type(list2_str)) print(type(list2)) str_list2=json.loads(list2_str) print(str_list2) print(type(str_list2)) # 元组的序列与反序列 # 元组的反序列化有点特殊他不会转换为元组而是转换为列表 tuple1=("dsy","wuya") tuple1_str=json.dumps(tuple1) print(tuple1_str) print(type(tuple1_str)) print(type(tuple1)) str_tuple1=json.loads(tuple1_str) print(str_tuple1) print(type(str_tuple1)) # 字典的序列化与反序列化 dict1={"name":"dsy","age":18} print(type(dict1)) dict1_str=json.dumps(dict1) print(dict1_str) print(type(dict1_str)) str_dict1=json.loads(dict1_str) print(str_dict1) print(type(str_dict1)) # 对文件的序列化与反序列化 json.dump(dict1,open('wenjian.txt','w',encoding='utf-8'),ensure_ascii=False) print(json.load(open('wenjian.txt','r',encoding='utf-8')))
输出结果:
hashlib库
代码
import hashlib from urllib import parse import time def sign(): data1={'name':'duanshuyuan','age':18,'work':'testDev','time':time.time()} # 对请求参数进行ascill码排序 data=dict(sorted(data1.items(),key=lambda item:item[0])) # 请求差数处理成key=value and key1=value1 and key2=value2 data=parse.urlencode(data) # 进行md5加密 m=hashlib.md5() # 要把字符串的数据处理成bytes数据类型 m.update(data.encode('utf-8')) return m.hexdigest() print(sign())
输出结果:
sys库
原来的搜索路径:
import sys for item in sys.path: print(item)
输出结果:
添加后的路径
代码:
import sys sys.path.append('D:/python3/python.exe D:/code/testDev/data') for item in sys.path: print(item)
输出结果:
time库
import time print('获取时间戳:',time.time()) print('当前时间字符串格式:',time.ctime()) # 休眠,秒为单位r # time.sleep(5) # print('hello world') localeTime=time.localtime(time.time()) print('年:{0},月:{1},日:{2},'.format(localeTime.tm_year,localeTime.tm_mon,localeTime.tm_mday)) print('中国人方式:',time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())) print('中国人方式:',time.strftime('%Y-%m-%d %x',time.localtime()))
输出结果
detetime库
''' 相比于time模块,datetime也是表示时间的,但是会更加直观的, ''' import datetime import time print('当前时间',datetime.datetime.now()) print('当前时间基础上增加n天或n小时',datetime.datetime.now()+datetime.timedelta(hours=1)) # 时间戳转换格式 # 给的是时间戳,需要处理成当前时间 print(datetime.datetime.fromtimestamp(time.time()))
输出结果:
原创文章,作者:1402239773,如若转载,请注明出处:https://blog.ytso.com/267060.html