FishC笔记—30 讲 文件系统,介绍一个高大上的东西
来源:互联网 发布:win10 windows安全性 编辑:程序博客网 时间:2024/05/29 03:19
本期内容详解:
os模块中关于文件/目录常用的函数使用方法
os.path模块中关于路径常用的函数使用方法
典型课后题
编写一个程序,统计当前目录下每个文件类型的文件数,程序实现如图:
参考答案:
import os all_files = os.listdir(os.curdir) # 使用os.curdir表示当前目录更标准 type_dict = dict() for each_file in all_files: if os.path.isdir(each_file): type_dict.setdefault('文件夹', 0) type_dict['文件夹'] += 1 else: ext = os.path.splitext(each_file)[1] type_dict.setdefault(ext, 0) type_dict[ext] += 1 for each_type in type_dict.keys(): print('该文件夹下共有类型为【%s】的文件 %d 个' % (each_type, type_dict[each_type]))
编写一个程序,计算当前文件夹下所有文件的大小,程序实现如图:
参考论坛的答案:
import os def file_size(): file_name = os.listdir(os.curdir) dict1 = dict() for each_file in file_name: if os.path.isfile(each_file): dict1.setdefault(each_file,os.path.getsize(each_file)) print('%s的大小为:【%d Bytes】'%(each_file,dict1[each_file])) file_size()
编写一个程序,用户输入文件名以及开始搜索的路径,搜索该文件是否存在。如遇到文件夹,则进入文件夹继续搜
索,程序实现如图:
参考答案:
import os def search_file(start_dir, target) : os.chdir(start_dir) for each_file in os.listdir(os.curdir) : if each_file == target : print(os.getcwd() + os.sep + each_file) # 使用os.sep是程序更标准 if os.path.isdir(each_file) : search_file(each_file, target) # 递归调用 os.chdir(os.pardir) # 递归调用后切记返回上一层目录 start_dir = input('请输入待查找的初始目录:') target = input('请输入需要查找的目标文件:') search_file(start_dir, target)
编写一个程序,用户输入开始搜索的路径,查找该路径下(包含子文件夹内)所有的视频格式文件(要求查找mp4,rmvb, avi的格式即可),并把创建一个文件(vedioList.txt)存放所有找到的文件的路径,程序实现如图:
感觉论坛的参考答案有点问题,修改如下:
import os vedio_list = [] def search_file(start_dir) : os.chdir(start_dir) for each_file in os.listdir(os.curdir) : if os.path.isfile(each_file) : file_ext = os.path.splitext(each_file)[1] if file_ext in ['.mp4','.rmvb','.avi']: vedio_list.append(os.getcwd() + os.sep + each_file + os.linesep) if os.path.isdir(each_file) : search_file(each_file) # 递归调用 os.chdir(os.pardir) # 递归调用后切记返回上一层目录 return vedio_list; start_dir = input('请输入待查找的初始目录:') vedio_list = search_file(start_dir) f = open(os.getcwd() + os.sep + 'VedioList.txt','w') f.writelines(vedio_list) f.close()
编写一个程序,用户输入关键字,查找当前文件夹内(如果当前文件夹内包含文件夹,则进入文件夹继续搜索)所有含
有该关键字的文本文件(.txt后缀),要求显示该文件所在的位置以及关键字在文件中的具体位置(第几行第几个字符),
程序实现如图:
参考答案:
import os def print_pos(key_dict): keys = key_dict.keys() keys = sorted(keys) # 由于字典是无序的,我们这里对行数进行排序 for each_key in keys: print('关键字出现在第 %s 行,第 %s 个位置。' % (each_key, str(key_dict[each_key]))) def pos_in_line(line, key): pos = [] begin = line.find(key) while begin != -1: pos.append(begin + 1) # 用户的角度是从1开始数 begin = line.find(key, begin+1) # 从下一个位置继续查找 return pos def search_in_file(file_name, key): f = open(file_name) count = 0 # 记录行数 key_dict = dict() # 字典,用户存放key所在具体行数对应具体位置 for each_line in f: count += 1 if key in each_line: pos = pos_in_line(each_line, key) # key在每行对应的位置 key_dict[count] = pos f.close() return key_dict def search_files(key, detail): all_files = os.walk(os.getcwd()) txt_files = [] for i in all_files: for each_file in i[2]: if os.path.splitext(each_file)[1] == '.txt': # 根据后缀判断是否文本文件 each_file = os.path.join(i[0], each_file) txt_files.append(each_file) for each_txt_file in txt_files: key_dict = search_in_file(each_txt_file, key) if key_dict: print('================================================================') print('在文件【%s】中找到关键字【%s】' % (each_txt_file, key)) if detail in ['YES', 'Yes', 'yes']: print_pos(key_dict) key = input('请将该脚本放于待查找的文件夹内,请输入关键字:') detail = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):' % key) search_files(key, detail)
0 0
- FishC笔记—30 讲 文件系统,介绍一个高大上的东西
- 第30讲:文件系统:介绍一个高大上的东西
- 030:文件系统,介绍一个高大上的东西
- FishC笔记—29 讲 文件:一个任务
- FishC笔记—10 讲 列表:一个打了激素的数组
- FishC笔记—11 讲 列表:一个打了激素的数组2
- FishC笔记—12 讲 列表:一个打了激素的数组3
- 14.文件系统:高大上的东西——import关键字/模块
- FishC笔记—04 讲:改进我们的小游戏
- FishC笔记—05 讲:闲聊之 Python 的数据类型
- FishC笔记—15 讲 字符串:格式化
- FishC笔记—16 讲 序列,序列
- FishC笔记—19 讲 函数:我的地盘听我的
- FishC笔记—34 讲 丰富的else语句及简的with语句
- FishC笔记—08 讲:了不起的分支和循环2
- FishC笔记—09 讲:了不起的分支和循环3
- FishC笔记—13 讲 元组:戴上枷锁的列表
- FishC笔记—14 讲 字符串:各种奇葩的内置方法
- 列出当前文件夹及其子文件夹下所有文件名称
- ***【杭电】5969最大位或
- linux下布署java和定时任务
- iOS端连接H5页面的时候手动增加Cookie的值
- 定时刷新(refresh)
- FishC笔记—30 讲 文件系统,介绍一个高大上的东西
- 安卓设置内存
- 充满神秘色彩的三星手机可折叠手机即将闪亮登场
- 利用nginx 反向代理本地解决跨域问题
- xcode中安装ruby环境
- Linux定时任务Crontab详解
- iOS同步锁
- codeforces round#404(div.2) A. Anton and Polyhedrons
- 天梯赛 5-9 抢红包