啊淡淡蛋蛋 发表于 2021-7-20 12:05:08

为什么我在F盘里直接运行报错


为什么我在F盘里直接运行报错,而自己创一个文件夹,在这个文件夹里面创文件夹和txt则可以正常运行呢,是因为我F盘装有游戏里某些文件不能打开的原因吗?

报错内容如下:(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 16: invalid start byte



这是我的F盘文件夹:
$RECYCLE.BIN
1
ceshi
CloudMusic
epic
Program Files (x86)
steam
System Volume Information
uu
微信
胡闹厨房2




import os
import os.path



def chengxu(x,y):
    os.chdir(x)
   
   

    for i in os.listdir():
      
      if os.path.splitext(i) == '.txt':
            q= os.getcwd() + os.sep + i
            f = open(i,'r' ,encoding='utf-8')
            if y in f.read():
                f = open(i,'r' ,encoding='utf-8')
                m =0
               
                for j in list(f):
                  f = open(i,'r' ,encoding='utf-8')
                  m +=1
                  if y in j:
                        start = j.index('我')
                        end = j.index('是')
                        print(f'在文件中{q}找到关键字{y}')
                        print(f'关键字出现在第{m}行,第[{start},{end}]个位置')
                        f.close()
            else:
                f.close()
                  
      if os.path.isdir(i) :
            chengxu(i,y)
            os.chdir(os.pardir)
      
x = input('目录:')

y = input('关键字:')
chengxu(x,y)

逃兵 发表于 2021-7-20 12:41:48

不是所有的txt文档编码都是utf-8
页: [1]
查看完整版本: 为什么我在F盘里直接运行报错