鱼C论坛

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

[技术交流] 旧版第030讲课后作业展示

[复制链接]
发表于 2022-10-26 18:56:35 | 显示全部楼层 |阅读模式

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

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

x
原帖地址:https://fishc.com.cn/thread-45649-1-1.html
本讲有关文件系统的操作编程,动动手共5道题目,如下是题目简述与小古比鱼编写的程序,感觉比小甲鱼老师提供的参考答案更简洁、更优雅!望各位鱼友走过路过,留下宝贵意见,共同交流进步!
0. 文件类型统计(统计当前目录下每个文件类型的文件数)

                               
登录/注册后可看大图

  1. import os
  2. ext = [os.path.splitext(file)[1] for file in os.listdir()]    # 将当前目录下各文件的扩展名存入列表ext
  3. files = set(ext)                                              # 将ext去重后转换为集合files(文件类型)
  4. for type in files:
  5.     print(f"该文件夹下共有类型为【{type if type else '文件夹'}】的文件",ext.count(type),'个')    # 使用f-字符串输出
复制代码

1. 文件大小统计(统计当前文件夹下所有文件的大小)

                               
登录/注册后可看大图

  1. import os
  2. for file in os.listdir():
  3.     print(file,f'【{os.path.getsize(file)}Bytes】')
复制代码

2. 文件查找(用户输入文件名以及开始搜索的路径,搜索该文件是否存在)

                               
登录/注册后可看大图

  1. import os
  2. path = input('请输入待查找的初始目录:')
  3. file = input('请输入需要查找的目标文件:')
  4. found = False
  5. for each in os.walk(path):
  6.     if file in each[2]:
  7.         print(os.path.join(each[0],file))
  8.         found = True
  9. if not found:
  10.     print('未查找到相关文件!')
复制代码

3. 视频查找(查找用户输入的路径下所有mp4、rmvb、avi格式的视频文件,将路径保存在文件“vedioList.txt”中)

                               
登录/注册后可看大图

  1. import os
  2. path = input('请输入待查找的初始目录:')
  3. found = []
  4. for each in os.walk(path):
  5.     for file in each[2]:
  6.         if file.endswith(('.mp4','.rmvb','.avi')):
  7.             found.append(os.path.join(each[0],file))

  8. if found:
  9.     f = open('vedioList_Guppy.txt','w')
  10.     f.writelines('\n'.join(found))
  11.     f.close()
  12. else:
  13.     print('未查找到相关文件!')
复制代码

4. 文本关键字查找(查找当前文件夹内所有含有用户输入的关键字的文本文件,附带显示关键字具体位置的功能)

                               
登录/注册后可看大图

  1. import os
  2. import chardet

  3. def check_charset(file):            # 函数check_charset()用于检测文本文件file的编码方式并返回
  4.     with open(file,'rb') as f:
  5.         data = f.read(4)
  6.         charset = chardet.detect(data)['encoding']
  7.     return 'gbk' if charset == 'ISO-8859-1' else 'utf-8'

  8. key = input('请将该脚本放于待查找的文件夹内,请输入关键字:')
  9. loc = input(f'请问是否需要打印关键字【{key}】在文件中的具体位置(YES/NO):') in ('YES','Yes','yes')
  10. found = False
  11. for each in os.walk('.'):
  12.     for file in each[2]:
  13.         if file.endswith('.txt'):
  14.             path = os.path.join(each[0],file)
  15.             f = open(path,encoding=check_charset(path))
  16.             str = f.read()
  17.             f.close()
  18.             if key in str:
  19.                 print('==================================================================')
  20.                 print(f'在文件【{path}】中找到关键字【{key}】')
  21.                 found = True
  22.                 if loc:
  23.                     list = str.split('\n')
  24.                     for line in range(len(list)):
  25.                         index = []
  26.                         start = 0
  27.                         while list[line].find(key,start) >= 0:
  28.                             index.append(list[line].find(key,start)+1)
  29.                             start = index[-1]
  30.                         if index:
  31.                             print(f'关键字出现在第 {line+1} 行,第 {index} 个位置。')
  32. if not found:
  33.     print(f'未找到含关键字【{key}】的文件!')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 01:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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