在所有目录中读取所有文件

16

我已经成功编写了代码,可以读取单个文本文件的值,但是在尝试读取所有目录中的所有文件并将所有内容组合在一起时遇到困难。

这是我目前所拥有的:

filename = '*'
filesuffix = '*'
location = os.path.join('Test', filename + "." + filesuffix)
Document = filename
thedictionary = {}
with open(location) as f:
 file_contents = f.read().lower().split(' ') # split line on spaces to make a list
 for position, item in enumerate(file_contents): 
     if item in thedictionary:
      thedictionary[item].append(position)
     else:
      thedictionary[item] = [position]
wordlist = (thedictionary, Document)
#print wordlist
#print thedictionary

请注意,我正在尝试为文件名和文件后缀都插入通配符*。我收到以下错误:

"IOError: [Errno 2] No such file or directory: 'Test/.'"

我不确定这是否是正确的方法,但如果我能让通配符正常工作,它应该可以解决问题。

我已经让这个例子工作了:Python - 从目录中读取文件,在子目录中找不到文件

这有点不同,但不知道如何更新它以读取所有文件。我认为在这一开始的代码集中:

previous_dir = os.getcwd()
os.chdir('testfilefolder')
#add something here?
for filename in os.listdir('.'):

我需要在外部for循环中添加一个东西,但不太清楚要放什么。

有任何想法吗?

1个回答

18

Python没有直接支持在文件名中使用通配符来调用open()函数。相反,您需要使用glob模块从单个子目录级别加载文件,或者使用os.walk()来遍历任意目录结构。

打开所有一级子目录中的文本文件:

import glob

for filename in glob.iglob(os.path.join('Test', '*', '*.txt')):
    with open(filename) as f:
        # one file open, handle it, next loop will present you with a new file.

打开任意嵌套目录中的所有文本文件:

import os
import fnmatch

for dirpath, dirs, files in os.walk('Test'):
    for filename in fnmatch.filter(files, '*.txt'):
        with open(os.path.join(dirpath, filename)):
            # one file open, handle it, next loop will present you with a new file.

谢谢Martijn。我会试一下看看会发生什么。我很好奇为什么他们要制作两个不同的函数glob和os.walk。经过一些阅读,我发现glob将允许您使用通配符,但os.walk不会 - 相反,您需要过滤结果。我不明白到底发生了什么,因为当我在考虑过滤结果时,我认为这就是通配符表达式所做的。我找到了这篇文章:https://dev59.com/KGox5IYBdhLWcg3w1X6-如果您有任何见解和时间,任何想法都会受到赞赏。 - Relative0
glob() 目前不支持任意嵌套子目录。这是唯一的区别。os.walk() 支持,但需要更多的过滤。请注意,glob() 在其自身的实现中已经使用了 相同的过滤方法fnmatch 模块)。 - Martijn Pieters

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接