没有这样的文件或目录:

时间:2019-09-30 04:13:36

标签: python

我想在python中打开多个文件,并且当我在代码中使用“ with open”时显示“没有这样的文件或目录” 我不知道为什么。

在我使用“ with open”之前,可以在目录中显示列表名称。

for i in finders.find("home/view", True):
        for t in os.listdir(i):
            print(t)

但是我使用“ with open”错误

    for i in finders.find("home", True):
        for t in os.listdir(i):
            with open(t, "r") as f:
                test = f.read()

我希望输出显示文件中的数据。我该怎么办?

1 个答案:

答案 0 :(得分:2)

您需要在文件夹路径中包含open。对os.listdir(app)的调用仅提供文件名。您需要在这些文件名前加上app中的路径。

with open(f'{app}/{files}', 'r') as f: