竹马_ 2022-06-27 09:39 采纳率: 80%
浏览 42
已结题

文件夹处理的查找操作

img


请问这个错误类型是什么意思?要怎么修改内容 不是很懂关于这里?

  • 写回答

4条回答 默认 最新

  • foreverliuyin 2022-06-27 10:00
    关注

    注意:

    1. 字符串后4位应为[-4:]
    2. with段缩进调整
    3. 搜索strkey缩进调整
    4. 函数应返回filepathlist
    import os
    
    
    def findfiles(path, strkey):
        filepathlist = []
        txt = ""
        dir_files = os.walk(path)
        for toppath, subpath, fileslist in dir_files:
            for file in fileslist:
                if file[-4:] == ".txt":
                    file_path = os.path.join(toppath, file)
                    with open(file_path, "r", encoding="utf-8") as f:
                        txt = f.read()
                if strkey in txt:
                    filepathlist.append(file_path)
        return filepathlist
    
    
    path = "/Users/apple/Desktop/python/python文件练习"
    strkey = "沧海"
    fileslist = findfiles(path, strkey)
    print(fileslist)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月27日
  • 已采纳回答 6月27日
  • 创建了问题 6月27日