|
发表于 2016-12-10 20:02:16
|
显示全部楼层
老师这个参考答案是有一个文件夹判断bug的。
给你一个修正版答案 很好理解。
- import os
- def funcation(contents):
- file_list = open(file_save,"a")
- temp = {}
- print("====================查询开始=======================")
- print(">>>>>>>>>>>>>>>>>>>>>查询中>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
- for each in os.walk(contents):
- for each_name in each[2]:
- if (os.path.splitext(each_name))[1].upper() in list_suf:
- file_list = open(file_save)
- r = file_list.read()
- file_list.close()
- if each_name not in r:
- file_list = open(file_save,"a")
- new_file = os.path.join(each[0],each_name)
- size = str(os.path.getsize(new_file))
- file_list.write(new_file+"\t"+"文件大小"+"->>"+size+"Bytes"+"\n\n")
- file_list.close()
- else:
- continue
- else:
- continue
- try:
- list_suf = [".TXT"]#需要检查的文件后缀
- file_save = "D:\\1.txt"#统计结果存放路径
- funcation("C:\")#主程序,需要检查的路径
- except FileNotFoundError as reason:
- #有些隐藏文件是访问不了
- print("该目录有无法限访问文件夹,文件如下:",reason)
- finally:
- print("====================查询完毕=======================")
- print("=================文件已经保存在:%s======"%(file_save))
复制代码 |
评分
-
查看全部评分
|