我已经成功编写了代码,可以读取单个文本文件的值,但是在尝试读取所有目录中的所有文件并将所有内容组合在一起时遇到困难。
这是我目前所拥有的:
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循环中添加一个东西,但不太清楚要放什么。
有任何想法吗?
glob()
目前不支持任意嵌套子目录。这是唯一的区别。os.walk()
支持,但需要更多的过滤。请注意,glob()
在其自身的实现中已经使用了 相同的过滤方法(fnmatch
模块)。 - Martijn Pieters