鱼C论坛

 找回密码
 立即注册
查看: 554|回复: 1

python小白,python新手学习30课课后作业。求助错误提示解决,标题放不下。

[复制链接]
发表于 2018-12-19 22:01:55 | 显示全部楼层 |阅读模式

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

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

x
  1. def search_file(start_path,target_file):
  2.         '''入查询范围和查询文件
  3.         返回真假值或路径'''
  4.         import os
  5.         f=os.walk(start_path)
  6.         for f_a in f:
  7.                 for f_a_a in f_a[2]:
  8.                         if target_file in f_a_a:
  9.                                 return [True,f_a[0]]
  10.         return [False,]
  11. def secrch_video(start_path):
  12.         '查询视频格式写入文件'
  13.         import os
  14.         f=open('F:\\资料\\vedioList.txt','a')
  15.         h=os.walk(start_path)
  16.         target_formats=['mp4','avi','rmvb']
  17.         for h_a in h:
  18.                 for h_a_a in h_a[2]:
  19.                         for target_format in target_formats:
  20.                                 if target_format in h_a_a:
  21.                                         f.wriet(h_a_a)
  22.         f.close()
  23. def find_keywords(start_path,keyword):
  24.         '查询txt文件关键字'
  25.         import os
  26.         files=os.walk(start_path)
  27.         a=0
  28.         for f_a in files:
  29.                 for f_a_a in f_a[2]:
  30.                         if 'txt' in f_a_a:
  31.                                 f_txts=open(f_a[0]+"\"+f_a_a)
  32.                                 for f_txt in f_txts:
  33.                                         a+=1
  34.                                         if keyword in f_txt:
  35.                                                 print(keyword+"在"+f_a[0]+"\"+f_a_a+"的"+str(a)+"行!")
  36.                                 f_txts.close()
  37.                                 a=0
  38.         print("**********************************")       
  39.                                        
  40. start_path='F:\\'#input("请输入查询范围:")
  41. target_file='hanoi.cpython-37.pyc'#input("请输入要查询的文件:")
  42. keyword= "小甲鱼" #input("请输入要查找的关键字:")
  43. a=search_file(start_path,target_file)
  44. #secrch_video(start_path)
  45. find_keywords(start_path,keyword)
  46. if a[0]:
  47.         print(a[1])
  48. else:
  49.         print('不存在')
复制代码

我不知道为什么,secrch_video(start_path)函数写入文件提示'_io.TextIOWrapper' object has no attribute 'write'
而且也没有创建一个在 F:\\资料\\vedioList.txt 路径上的txt文件
find_keywords(start_path,keyword)函数在给的路径为D:\\时提示:'gbk'  codec can't decode byte 0xc0 in position 16: illegal multibyte sequence
但在给更下一级的路径时可以运行       
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-12-19 22:16:30 | 显示全部楼层
我实在是糊涂了,翻译了'gbk'  codec can't decode byte 0xc0 in position 16: illegal multibyte sequence 为第0xc0位置有一个非法的多字节,gbk编码库无法翻译。好歹该我一个乱码的翻译都行啊。什么东西无法翻译啊。txt文件我用windows自带的记事本都能打开啊,虽然有的是乱码。
而'_io.TextIOWrapper' object has no attribute 'write'报错我就彻底懵比了,为什么啊
没有读写属性?
我估计靠我自己是解决不了了,所以在这里厚着脸皮求助各位大佬,前辈们。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-12 12:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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