鱼C论坛

 找回密码
 立即注册
查看: 1525|回复: 0

[作品展示] 【字典】获取课程文件夹列表写入txt,对学习状态进行管理

[复制链接]
发表于 2019-5-5 14:23:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 waletor 于 2019-5-7 21:10 编辑

1、将课程文件夹列表写入字典,存入txt
2、txt读取返回字典
3、修改字典的值,并写入txt

实现txt如下效果:
000愉快的开始,已学
001我和Python的第一次亲密接触,已学
002用Python设计第一个游戏,已学
003小插曲之变量和字符串,已学
004改进我们的小游戏,已学
005闲聊之Python的数据类型,已学



  1. import os

  2. #获取文件列表
  3. def getList(path,workList):
  4.     for name in os.listdir(path):
  5.         fullpath=os.path.join(path,name)
  6.         if os.path.isdir(fullpath):
  7.             workList.setdefault(name,'未学')

  8. #字典写入txt
  9. def writetxt(workList):
  10.     with open ("record.txt",'w+') as f:
  11.         for each in workList.items():
  12.             strline=each[0]+","+each[1]+'\n'
  13.             f.writelines(strline)
  14.             
  15. #txt写入字典
  16. def readtxt(x):
  17.     with open ("record.txt",'r+') as f:
  18.         for each in f:
  19.             key=each.split(",")[0]
  20.             value=each.split(",")[1].strip('\n')
  21.             x[key]=value   
  22.     return x

  23. #展示课程状态
  24. def show():
  25.     if os.path.exists("record.txt")==False:
  26.         x={}
  27.         getList('.',x)
  28.         writetxt(x)
  29.     a={}
  30.     a=readtxt(a)
  31.     count=0
  32.     total=len(a.keys())
  33.     for each in a.items():  
  34.         if each[1]=="已学":
  35.             count+=1
  36.         print (each)
  37.         
  38.     percent=count/total*100
  39.     print("总共%d节课,已学习%d节,未学习%d节,学习完成%d%%" %(total,count,total-count,percent))   
  40.     return a

  41. #设置课题的状态值
  42. def setValue(key):
  43.     a=show()   
  44.     a[key]='已学'
  45.     writetxt(a)
  46.    
  47. a=show()      
  48. setValue("026字典:当索引不好用时2")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-17 06:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表