os 模块
os.path.abspath 规范绝对路径
os.path.split() 把路径分成两段,第二段是一个文件或者是文件夹
os.path.dirname 取第一部分
os.path.basename 取第二部分
os.path.exists() 判断文件或者文件夹是否存在 True Flase
os.path.isabs() 判断是否是绝对路径,是的话返回True
os.path.isfile() 判断是否是一个文件,是返回True
os.path.isdir() 判断是一个存在的目录,是返回True
os.path.join(path1,path2) 将多个路径组合返回
os.path.getatime() 返回path所指向的文件或者目录的最后访问时间
os.path.getmtime () 返回path对指向的文件或者目录的最后修改时间
os.path.getsize() 返回path的大小
# by luffycity.com import os # 把路径中不符合规范的/改成操作系统默认的格式 # path = os.path.abspath('D:/sylar/s15/day19/4.os模块.py') # print(path) # 能够给能找到的相对路径改成绝对路径 # path = os.path.abspath('4.os模块.py') # print(path) # 就是把一个路径分成两段,第二段是一个文件/文件夹 # path= os.path.split('D:/sylar/s15/day19/4.os模块.py') # print(path) # path= os.path.split('D:/sylar/s15/day19') # print(path) # ret1 = os.path.dirname('D:/sylar/s15/day19/4.os模块.py') # ret2 = os.path.basename('D:/sylar/s15/day19/4.os模块.py') # print(ret1) # print(ret2) # 如果你两个值都需要 os.path.split # 如果你只要一个值 os.path.dirname/os.path.basename # 判断文件/文件夹是否存在 # res = os.path.exists(r'D:sylars15day194.os模块.py') # print(res) # res1 = os.path.isabs('4.os模块.py') # res2 = os.path.isabs(r'D:sylars15day194.os模块.py') # print(res1) # print(res2) # print(os.listdir('D:sylars15')) # print(os.path.isdir(r'D:sylars15aaa')) # print(os.path.isfile(r'D:sylars15aaa')) # print(os.path.isfile('D:sylars15day01')) # print(os.path.isdir('D:sylars15day01')) # path = os.path.join('D:sylars15','bbb') # print(path) # size= os.path.getsize(r'D:sylars15day194.os模块.py') # 查看文件大小 # print(size) # ret1 = os.path.getsize('D:sylars15day19') # ret2 = os.path.getsize('D:sylars15day18') # ret3 = os.path.getsize('D:sylars15day17') # ret4 = os.path.getsize('D:sylars15') # print(ret1,ret2,ret3,ret4) # 所有的文件夹 都至少是4096个字节 # 8192 # 64字节 + 32字节/新文件 # 使用python代码统计一个文件夹中所有文件的总大小 # 你需要统计文件夹大小 # D:sylars15 文件夹的大小 # 拿到这个文件夹下所有的文件夹 和 文件 # 是文件就取大小 # 是文件夹 再打开这个文件夹 : 文件/文件夹 # 递归 def func(path): # r'D:sylars15' size_sum = 0 name_lst = os.listdir(path) for name in name_lst: path_abs = os.path.join(path,name) if os.path.isdir(path_abs): size = func(path_abs) size_sum += size else: size_sum += os.path.getsize(path_abs) return size_sum ret = func(r'D:sylars15') print(ret) # def func(path): # D:sylars15 # size_sum = 0 # 0 # name_lst = os.listdir(path) # ['day01','day02',...] # for name in name_lst: # 'day01' 'day02' # path = os.path.join(path,name) # 'D:sylars15day01' # if os.path.isdir(path): # True # size = func(path) # func('D:sylars15day01') # size_sum += size # else: # size_sum += os.path.getsize(path) # return size_sum # # def func(path): # 'D:sylars15day02' # size_sum = 0 # sum = 0 # name_lst = os.listdir(path) # [first.py...] # for name in name_lst: # first.py 100 # path = os.path.join(path,name) # if os.path.isdir(path): # func(path) # else: # 文件 # size_sum += os.path.getsize(path) # sizesum += 100 = 100+100+100+100 = 500 # return size_sum # return 500 # 循环 # 堆栈思想 # 列表 满足一个顺序 先进来的后出去 lst = [r'D:sylars15',] # 列表的第一个目录就是我要统计的目录 size_sum = 0 while lst: # [r'D:sylars15',] lst = ['D:sylars15day01','D:sylars15day01'..] path = lst.pop() # path = 'D:sylars15' lst = [] path_list = os.listdir(path) # path_list = ['day01',day02',aaa,day15.py] for name in path_list: # name = day01 abs_path = os.path.join(path,name) if os.path.isdir(abs_path): # 文件夹的逻辑 lst.append(abs_path) # lst.append('D:sylars15day01') lst = ['D:sylars15day01'] else: size_sum += os.path.getsize(abs_path) print(size_sum)
序列化模块:
序列化:把其它的数据类型转化为字符串 、 bytes 序列化的过程、
序列化: 字符串、bytes
序列: 列表、元组、字符串、bytes
1 # by luffycity.com 2 3 # python 4 # 序列化 : 字符串 bytes 5 # 序列 : 列表 元组 字符串 bytes 6 7 # 把其他的数据类型 转换成 字符串 bytes 序列化的过程 8 9 # str 10 # dic = {'1':'2'} 11 # print([str(dic),dic]) 12 # print([str([1,2,3]),[1,2,3]]) 13 14 # 为什么要把其他数据类型转换成字符串??? 15 # 能够在网络上传输的只能是bytes, 16 # 能够存储在文件里的只有bytes和str 17 # dic = {'小明':{'phone_num':123123123123,}} 18 # ''' 19 # 小明|电话|性别 20 # 小张|... 21 # ''' 22 # 字典 -> 字符串 -通过网络去传输-> 字符串 -> 字典 23 24 # 转字符串的过程 不就是数据类型的强制转换么?为什么要学个序列化模块? 25 # 字符串 -> 字典 26 # str_dic = str([1,2,3]) 27 # print(str_dic,type(str_dic)) 28 29 30 # 文件中读出来的 网络上接收来的 31 # res = eval(str_dic) 32 # print(res,type(res)) 33 34 # eval 要谨慎的使用,用户的输入/网络上接收的数据/文件中的内容 35 # eval('import os;os.remove('c:')') 36 # eval('import urllib;') 37 38 # 你已知的代码 但是可能需要一些拼接 根据你自己的逻辑去做的拼接 39 40 41 # json 42 # pickle
json 模块: 是所有语言都可以识别的
四个功能:dumps、dump、doads、load
1 # by luffycity.com 2 # dic = {'key' : 'value','key2' : 'value2'} 3 # import json 4 # ret = json.dumps(dic) # 序列化 5 # print(dic,type(dic)) 6 # print(ret,type(ret)) 7 8 # res = json.loads(ret) # 反序列化 9 # print(res,type(res)) 10 11 # 问题1 12 # dic = {1 : 'value',2 : 'value2'} 13 # ret = json.dumps(dic) # 序列化 14 # print(dic,type(dic)) 15 # print(ret,type(ret)) 16 # 17 # res = json.loads(ret) # 反序列化 18 # print(res,type(res)) 19 20 # 问题2 21 # dic = {1 : [1,2,3],2 : (4,5,'aa')} 22 # ret = json.dumps(dic) # 序列化 23 # print(dic,type(dic)) 24 # print(ret,type(ret)) 25 26 # res = json.loads(ret) # 反序列化 27 # print(res,type(res)) 28 29 # 问题3 30 # s = {1,2,'aaa'} 31 # json.dumps(s) 32 33 # 问题4 # TypeError: keys must be a string 34 # json.dumps({(1,2,3):123}) 35 36 # json 在所有的语言之间都通用 : json序列化的数据 在python上序列化了 那在java中也可以反序列化 37 # 能够处理的数据类型是非常有限的 : 字符串 列表 字典 数字 38 # 字典中的key只能是字符串 39 40 # 后端语言 java c c++ c# 41 # 前端语言 在网页上展示 42 43 # 向文件中记录字典 44 import json 45 # dic = {'key' : 'value','key2' : 'value2'} 46 # ret = json.dumps(dic) # 序列化 47 # with open('json_file','a') as f: 48 # f.write(ret) 49 50 # 从文件中读取字典 51 # with open('json_file','r') as f: 52 # str_dic = f.read() 53 # dic = json.loads(str_dic) 54 # print(dic.keys()) 55 56 # dump load 是直接操作文件的 57 # dic = {'key1' : 'value1','key2' : 'value2'} 58 # with open('json_file','a') as f: 59 # json.dump(dic,f) 60 61 # with open('json_file','r') as f: 62 # dic = json.load(f) 63 # print(dic.keys()) 64 65 # 问题5 不支持连续的存 取 66 # dic = {'key1' : 'value1','key2' : 'value2'} 67 # with open('json_file','a') as f: 68 # json.dump(dic,f) 69 # json.dump(dic,f) 70 # json.dump(dic,f) 71 72 # with open('json_file','r') as f: 73 # dic = json.load(f) 74 # print(dic.keys()) 75 76 # 需求 :就是想要把一个一个的字典放到文件中,再一个一个取出来??? 77 # dic = {'key1' : 'value1','key2' : 'value2'} 78 # 79 # with open('json_file','a') as f: 80 # str_dic = json.dumps(dic) 81 # f.write(str_dic+' ') 82 # str_dic = json.dumps(dic) 83 # f.write(str_dic + ' ') 84 # str_dic = json.dumps(dic) 85 # f.write(str_dic + ' ') 86 87 # with open('json_file','r') as f: 88 # for line in f: 89 # dic = json.loads(line.strip()) 90 # print(dic.keys()) 91 92 # json 93 # dumps loads 94 # 在内存中做数据转换 : 95 # dumps 数据类型 转成 字符串 序列化 96 # loads 字符串 转成 数据类型 反序列化 97 # dump load 98 # 直接将数据类型写入文件,直接从文件中读出数据类型 99 # dump 数据类型 写入 文件 序列化 100 # load 文件 读出 数据类型 反序列化 101 # json是所有语言都通用的一种序列化格式 102 # 只支持 列表 字典 字符串 数字 103 # 字典的key必须是字符串 104 105 106 # dic = {'key':'你好'} 107 # print(json.dumps(dic,ensure_ascii=False)) 108 109 # import json 110 # data = {'username':['李华','二愣子'],'sex':'male','age':16} 111 # json_dic2 = json.dumps(data,sort_keys=True,indent=4,separators=(',',':'),ensure_ascii=False) 112 # print(json_dic2) 113 114 # 存文件/传网络
1 import json 2 f = open('file','w') 3 json.dump({'国籍':'中国'},f) 4 ret = json.dumps({'国籍':'中国'}) 5 f.write(ret+' ') 6 json.dump({'国籍':'美国'},f,ensure_ascii=False) 7 ret = json.dumps({'国籍':'美国'},ensure_ascii=False) 8 f.write(ret+' ') 9 f.close() 10 11 ensure_ascii关键字参数
pickle模块:
是python特有的类型,和python的数据类型间进行转换
1 # by luffycity.com 2 import pickle 3 # 支持在python中几乎所有的数据类型 4 dic = {(1,2,3):{'a','b'},1:'abc'} 5 6 # ret = pickle.dumps(dic) 7 # print(ret) 8 #2. dumps 序列化的结果只能是字节 9 # print(pickle.loads(ret)) 10 # 3.只能在python中使用 11 # 4.在和文件操作的时候,需要用rb wb的模式打开文件 12 # 5.可以多次dump 和 多次load 13 # dump 14 # with open('pickle_file','wb') as f: 15 # pickle.dump(dic,f) 16 17 # load 18 # with open('pickle_file','rb') as f: 19 # ret = pickle.load(f) 20 # print(ret,type(ret)) 21 22 # dic = {(1,2,3):{'a','b'},1:'abc'} 23 # dic1 = {(1,2,3):{'a','b'},2:'abc'} 24 # dic2 = {(1,2,3):{'a','b'},3:'abc'} 25 # dic3 = {(1,2,3):{'a','b'},4:'abc'} 26 # with open('pickle_file','wb') as f: 27 # pickle.dump(dic, f) 28 # pickle.dump(dic1, f) 29 # pickle.dump(dic2, f) 30 # pickle.dump(dic3, f) 31 32 # with open('pickle_file','rb') as f: 33 # ret = pickle.load(f) 34 # print(ret,type(ret)) 35 # ret = pickle.load(f) 36 # print(ret,type(ret)) 37 # ret = pickle.load(f) 38 # print(ret, type(ret)) 39 # ret = pickle.load(f) 40 # print(ret, type(ret)) 41 # ret = pickle.load(f) 42 # print(ret, type(ret)) 43 44 with open('pickle_file','rb') as f: 45 while True: 46 try: 47 ret = pickle.load(f) 48 print(ret,type(ret)) 49 except EOFError: 50 break