如何在python中使用os.scandir打开目录中的所有文件?

如何在python中使用os.scandir打开目录中的所有文件?,python,directory,scandir,Python,Directory,Scandir,如何使用os.scandir打开目录中的所有文件? 我已经做了以下工作,但我的问题是,只有一个文件被一次又一次地重复,而不是每个文件 with os.scandir('/Users/names/Desktop/folder') as it: for entry in it: if entry.name.endswith(".txt") and fil.is_file(): path=entry.path

如何使用os.scandir打开目录中的所有文件? 我已经做了以下工作,但我的问题是,只有一个文件被一次又一次地重复,而不是每个文件

with os.scandir('/Users/names/Desktop/folder') as it:
    for entry in it:
        if entry.name.endswith(".txt") and fil.is_file():
            path=entry.path
            with open(path) as f:
                document=f.read()
                print(document)

你的代码对我有用,也许这取决于你的脚本在哪里。您的脚本是用“C:\`”编写的吗?