【问题标题】:My search thorugh files seems to get indexerror我对文件的搜索似乎得到了 indexerror
【发布时间】:2018-07-29 13:10:54
【问题描述】:

我收到的错误消息是 IndexError: tuple index out of range。 这很奇怪,因为我在这段代码中没有使用元组,我应该如何解决它?

import os
import glob
import pathlib

lista = []
word = input("Write searchword: ")
file_path = input("Write pathway: ")
for path, directories, files in os.walk(file_path):
    count = 0
    if os.path.isdir(file_path):
        for p in pathlib.Path(file_path).glob("."):
            with open(p) as user:
                    for line in user:
                        if word in line:
                                count += 1
            if count > 0:
                new = str(p).split("'")
                lista.append(new)
                lista.append(count)
                lista.append("\n")
        name = ' '.join(str(w) for w in lista)
        print (name)

【问题讨论】:

  • 请添加完整的回溯,以便我们查看错误的来源。

标签: python python-3.x python-2.7 python-requests


【解决方案1】:

如果您已阅读并发布了完整的 Traceback,那么您会看到 IndexError: tuple index out of range 位于 glob() 中,而不是您的代码。

错误在您的glob() 构造中,在那个“。”中不是一个可用的模式,我建议:

for p in pathlib.Path(file_path).glob("./*"):

但我不得不怀疑glob() 在这种情况下是否是一种好技术。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 2012-01-20
    相关资源
    最近更新 更多